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

Commit e36bc1b

Browse files
[C++ API] Step 10: add the new ATen tensor conversion files
This PR adds the files implementing the ATen user-facing tensor conversion. This will avoid leaking DLManagedTensors and ScopeGuard at each call site. As part of the bigger refactoring, these files are not yet activated (or even compiled) therefore the changeset cannot be tested independently. This new implementation will be switched in as part of the last commit of the global refactoring.
1 parent 2da97d1 commit e36bc1b

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

tc/aten/aten-inl.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <string>
19+
#include <vector>
20+
21+
#include <ATen/ATen.h>
22+
#include <ATen/DLConvertor.h>
23+
24+
#include "tc/core/tensor.h"
25+
26+
namespace tc {
27+
namespace aten {
28+
inline std::vector<DLTensorUPtr> makeDLTensors(
29+
const std::vector<at::Tensor>& tensors) {
30+
std::vector<DLTensorUPtr> dlTensors;
31+
for (auto tensor : tensors) {
32+
auto dlMTensor = at::toDLPack(tensor);
33+
dlTensors.push_back(makeDLTensor(&(dlMTensor->dl_tensor)));
34+
dlMTensor->deleter(dlMTensor);
35+
}
36+
return dlTensors;
37+
}
38+
39+
inline std::vector<DLConstTensorUPtr> makeDLConstTensors(
40+
const std::vector<at::Tensor>& tensors) {
41+
std::vector<DLConstTensorUPtr> dlTensors;
42+
for (auto tensor : tensors) {
43+
auto dlMTensor = at::toDLPack(tensor);
44+
dlTensors.push_back(makeDLConstTensor(&(dlMTensor->dl_tensor)));
45+
dlMTensor->deleter(dlMTensor);
46+
}
47+
return dlTensors;
48+
}
49+
} // namespace aten
50+
} // namespace tc

tc/aten/aten.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <string>
19+
#include <vector>
20+
21+
#include <ATen/ATen.h>
22+
23+
#include "tc/core/tensor.h"
24+
25+
namespace tc {
26+
namespace aten {
27+
28+
inline std::vector<DLTensorUPtr> makeDLTensors(
29+
const std::vector<at::Tensor>& tensors);
30+
31+
inline std::vector<DLConstTensorUPtr> makeDLConstTensors(
32+
const std::vector<at::Tensor>& tensors);
33+
34+
} // namespace aten
35+
} // namespace tc
36+
37+
#include "tc/aten/aten-inl.h"

0 commit comments

Comments
 (0)