forked from fsprojects/Furnace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicTensorOpsPerfPython.fs
More file actions
124 lines (103 loc) · 3.76 KB
/
BasicTensorOpsPerfPython.fs
File metadata and controls
124 lines (103 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2016- University of Oxford (Atilim Gunes Baydin <gunes@robots.ox.ac.uk>)
// and other contributors, see LICENSE in root of repository.
//
// BSD 2-Clause License. See LICENSE in root of repository.
namespace Furnace.Benchmarks.Python
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Configs
open Furnace.Benchmarks
open System
open Python.Runtime
[<AutoOpen>]
module PythonHelpers =
// take the lock
let gil = Py.GIL()
let scope = Py.CreateScope()
// your mileage may differ
if Environment.GetEnvironmentVariable("COMPUTERNAME") = "MSRC-3617253" then
Environment.SetEnvironmentVariable("PYTHONHOME", @"C:\ProgramData\Anaconda3\", EnvironmentVariableTarget.User)
if isNull (Environment.GetEnvironmentVariable "PYTHONHOME") then failwith "expect PYTHONHOME to be set"
let _prepPython = scope.Exec("import torch")
let execPython(code) =
scope.Exec(code) |> ignore
//[<ShortRunJob>]
[<MarkdownExporterAttribute.GitHub; AsciiDocExporter; HtmlExporter; CsvExporter; RPlotExporter>]
[<GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)>]
[<CategoriesColumn; BaselineColumn>]
type BasicTensorOps() =
inherit BasicTensorTestMatrix()
// The tests here must match the ones above
[<Benchmark; BenchmarkCategory("fromCpuData")>]
member perf.fromCpuData_PyTorch() =
let n = perf.numIterations(2)
execPython(sprintf """
for x in range(%d):
torch.tensor(range(%d), dtype=torch.%s, device="%s")
""" n perf.tensorSize perf.dtypeName perf.deviceName )
#if !TINY
[<Benchmark; BenchmarkCategory("zeros")>]
member perf.zeros_PyTorch() =
let n = perf.numIterations(10)
execPython(sprintf """
res = torch.tensor(1)
for x in range(%d):
res = torch.zeros(%d, dtype=torch.%s, device="%s")
""" n perf.tensorSize perf.dtypeName perf.deviceName )
[<Benchmark; BenchmarkCategory("ones")>]
member perf.ones_PyTorch() =
let n = perf.numIterations(10)
execPython(sprintf """
import torch
res = torch.tensor(1)
for x in range(%d):
res = torch.ones(%d, dtype=torch.%s, device="%s")
""" n perf.tensorSize perf.dtypeName perf.deviceName )
[<Benchmark; BenchmarkCategory("rand")>]
member perf.rand_PyTorch() =
let n = perf.numIterations(10)
execPython(sprintf """
import torch
res = torch.tensor(1)
for x in range(%d):
res = torch.rand(%d, dtype=torch.%s, device="%s")
""" n perf.tensorSize perf.dtypeName perf.deviceName )
[<Benchmark; BenchmarkCategory("addition")>]
member perf.addition_PyTorch() =
let n = perf.numIterations(10)
execPython(sprintf """
t = torch.tensor(range(%d), dtype=torch.%s, device="%s")
res = t
for x in range(%d):
res = t + t
""" perf.tensorSize perf.dtypeName perf.deviceName n )
[<Benchmark; BenchmarkCategory("addInPlace")>]
member perf.addInPlace_PyTorch() =
let n = perf.numIterations(10)
execPython(sprintf """
import torch
t = torch.tensor(range(%d), dtype=torch.%s, device="%s")
res = t
for x in range(%d):
res = t.add_(t)
""" perf.tensorSize perf.dtypeName perf.deviceName n )
[<Benchmark; BenchmarkCategory("addWithAlpha")>]
member perf.addWithAlpha_PyTorch() =
let n = perf.numIterations(10)
execPython(sprintf """
import torch
t = torch.tensor(range(%d), dtype=torch.%s, device="%s")
res = t
for x in range(%d):
res = t.add(t, alpha=3)
""" perf.tensorSize perf.dtypeName perf.deviceName n )
[<Benchmark; BenchmarkCategory("addScalar")>]
member perf.addScalar_PyTorch() =
let n = perf.numIterations(10)
execPython(sprintf """
import torch
t = torch.tensor(range(%d), dtype=torch.%s, device="%s")
res = t
for x in range(%d):
res = t + 1
""" perf.tensorSize perf.dtypeName perf.deviceName n )
#endif