Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit b77b583

Browse files
Merge pull request #545 from nicolasvasilache/pr/caffe2-update
Update to support upcoming Caffe2 API
2 parents f306bee + 379a450 commit b77b583

File tree

4 files changed

+48
-43
lines changed

4 files changed

+48
-43
lines changed

python/benchmarks/caffe2_benchmark.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import numpy as np
2020
import os
2121
import tensor_comprehensions as tc
22+
import torch
2223

2324
from caffe2.python import core, dyndep, workspace, utils
2425

@@ -85,18 +86,22 @@ def main():
8586

8687
@utils.debug
8788
def tune(args):
88-
fc = tc.define(FC_LANG, name="func_fc")
89-
options = fc.autotune(
90-
(args.batch_size, args.input_dim),
91-
(args.output_dim, args.input_dim),
92-
(args.output_dim,),
93-
cache = args.tuner_cache_file,
94-
threads = args.tuner_threads,
95-
generations = args.tuner_gen_generations,
96-
pop_size = args.tuner_gen_pop_size,
97-
)
98-
print(options.toString())
99-
return options
89+
tuner_config = (
90+
tc.TunerConfig()
91+
.generations(args.tuner_gen_generations)
92+
.devices(args.tuner_devices)
93+
.threads(args.tuner_threads)
94+
.pop_size(args.tuner_gen_pop_size))
95+
return tc.autotune(
96+
FC_LANG,
97+
'func_fc',
98+
torch.randn(args.batch_size, args.input_dim, device='cuda'),
99+
torch.randn(args.output_dim, args.input_dim, device='cuda'),
100+
torch.randn(args.output_dim, device='cuda'),
101+
starting_options = tc.MappingOptions('naive'),
102+
tuner_config = tuner_config,
103+
cache_filename = args.tuner_cache_file,
104+
store_to_cache = True)
100105

101106

102107
@utils.debug

python/examples/min_distance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ def argmin_1d(float(N) S, int32(D) MinIdx) -> (min_idx) {
103103

104104
T = tc.define(lang, tc.make_naive_options_factory())
105105
S = T.reduce_codes(luts_t, codes_t)
106-
V = T.min_2d(S.view((D, N / D)))
106+
V = T.min_2d(S.view((D, N // D)))
107107
v = T.min_1d(V)
108-
MinIdx = T.argmin_2d(S.view((D, N / D)), v)
108+
MinIdx = T.argmin_2d(S.view((D, N // D)), v)
109109
min_idx = T.argmin_1d(S, MinIdx)
110110
print("minval: {} minidx: {}".format(v, min_idx))
111111

@@ -159,8 +159,8 @@ def argmin_1d(float(K, N) S, int32(K, D) MinIdx2) -> (MinIdx) {
159159

160160
T = tc.define(lang, tc.make_naive_options_factory())
161161
S = T.reduce_codes(luts_t, codes_t)
162-
V2 = T.min_2d(S.view((K, D, N / D)))
162+
V2 = T.min_2d(S.view((K, D, N // D)))
163163
V = T.min_1d(V2)
164-
MinIdx2 = T.argmin_2d(S.view((K, D, N / D)), V)
164+
MinIdx2 = T.argmin_2d(S.view((K, D, N // D)), V)
165165
MinIdx = T.argmin_1d(S, MinIdx2)
166166
print("minval: {} minidx: {}".format(V, MinIdx))

python/tests/test_caffe2.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import hypothesis.strategies as st
1919
import caffe2.python.hypothesis_test_util as hu
2020
import tensor_comprehensions as tc
21+
import torch
2122

2223
from hypothesis import given, settings
2324
from caffe2.python import core, dyndep
@@ -33,9 +34,6 @@
3334
def matmul(float(M,N) A, float(N,K) B) -> (output) {
3435
output(m, k) +=! A(m, r_n) * B(r_n, k)
3536
}
36-
"""
37-
38-
MATMUL_GRAD_LANG = """
3937
def matmul_grad(float(M, N) A, float(N, K) B, float(M, K) d_O) -> (d_A, d_B) {
4038
d_A(m, n) +=! d_O(m, r_k) * B(n, r_k)
4139
d_B(n, k) +=! d_O(r_m, k) * A(r_m, n)
@@ -61,7 +59,7 @@ def ref(X, W):
6159
"TcOp", ["X", "Y"], "out",
6260
tc_def=MATMUL_LANG,
6361
tc_name="matmul",
64-
tc_grad_def=MATMUL_GRAD_LANG,
62+
tc_grad_def=MATMUL_LANG,
6563
tc_grad_name="matmul_grad",
6664
inputs_used_by_gradient=[0, 1],
6765
output_gradients_used_by_gradient=[0],
@@ -91,24 +89,23 @@ def ref(X, W):
9189
**hu.gcs_gpu_only)
9290
@settings(max_examples=2)
9391
def test_matmul_tune_and_run(self, n, m, k, seed, gc, dc):
94-
matmul = tc.define(MATMUL_LANG, name="matmul")
95-
matmul_grad = tc.define(MATMUL_GRAD_LANG, name="matmul_grad")
96-
97-
mapping_options = matmul.autotune(
98-
(n, k), (k, m),
99-
generations=3,
100-
threads=32,
101-
pop_size=2,
102-
tuner_min_launch_total_threads=1,
103-
)
104-
105-
grad_mapping_options = matmul_grad.autotune(
106-
(n, k), (k, m), (n, m),
107-
generations=1,
108-
threads=32,
109-
pop_size=2,
110-
tuner_min_launch_total_threads=1,
111-
)
92+
tuner = tc.Tuner(MATMUL_LANG)
93+
tuner_config = (
94+
tc.TunerConfig().generations(3).threads(32).pop_size(2)
95+
.tuner_min_launch_total_threads(1))
96+
matmul_top1 = tuner.tune(
97+
'matmul',
98+
(torch.randn(n, k, device='cuda'),
99+
torch.randn(k, m, device='cuda')),
100+
tc.MappingOptions('naive'),
101+
tuner_config)
102+
matmul_grad_top1 = tuner.tune(
103+
'matmul_grad',
104+
(torch.randn(n, k, device='cuda'),
105+
torch.randn(k, m, device='cuda'),
106+
torch.randn(n, m, device='cuda')),
107+
tc.MappingOptions('naive'),
108+
tuner_config)
112109

113110
X = np.random.rand(m, k).astype(np.float32)
114111
W = np.random.rand(k, n).astype(np.float32)
@@ -120,13 +117,13 @@ def ref(X, W):
120117
"TcOp", ["X", "Y"], "out",
121118
tc_def=MATMUL_LANG,
122119
tc_name="matmul",
123-
tc_grad_def=MATMUL_GRAD_LANG,
120+
tc_grad_def=MATMUL_LANG,
124121
tc_grad_name="matmul_grad",
125122
inputs_used_by_gradient=[0, 1],
126123
output_gradients_used_by_gradient=[0],
127124
inputs_to_compute_gradients_of=[0, 1],
128-
mapping_options=mapping_options.serialize(),
129-
grad_mapping_options=grad_mapping_options.serialize(),
125+
mapping_options=matmul_top1.serialize(),
126+
grad_mapping_options=matmul_grad_top1.serialize(),
130127
)
131128

132129
self.assertReferenceChecks(

test/caffe2/test_harness-inl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ caffe2::Tensor<typename Caffe2Backend::Context> GetNamedTensor(
6969
caffe2::Workspace& ws,
7070
const std::string& name) {
7171
// Resolved dynamically
72-
return caffe2::Tensor<typename Caffe2Backend::Context>(
73-
ws.GetBlob(name)->Get<typename Caffe2Backend::Tensor>());
72+
auto& t = ws.GetBlob(name)->Get<typename Caffe2Backend::Tensor>();
73+
caffe2::Tensor<typename Caffe2Backend::Context> res;
74+
res.ResizeLike(t);
75+
res.ShareData(t);
76+
return res;
7477
}
7578

7679
// helper functions to construct an ATen tensor from a caffe2 tensor

0 commit comments

Comments
 (0)