mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os
def get_compiled_files_list():
import json
with open("build/compile_commands.json") as f:
data = json.load(f)
files = [os.path.relpath(node['file']) for node in data]
for idx, fname in enumerate(files):
if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
return files
def run_clang_tidy(fname):
check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
changes = check_output(["git", "ls-files", "-m"])
if len(changes) == 0:
return
check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])
def main():
git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
compiled_files = get_compiled_files_list()
for idx, fname in enumerate(git_files):
if fname not in compiled_files:
continue
if fname.startswith("caffe2/contrib/aten/"):
continue
print(f"[{idx}/{len(git_files)}] Processing {fname}")
run_clang_tidy(fname)
if __name__ == "__main__":
main()
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892
Reviewed By: H-Huang
Differential Revision: D27991944
Pulled By: malfet
fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
92 lines
3.5 KiB
C++
92 lines
3.5 KiB
C++
#include "pybind_state_dlpack.h"
|
|
|
|
namespace caffe2 {
|
|
namespace python {
|
|
|
|
namespace py = pybind11;
|
|
|
|
const DLDeviceType* CaffeToDLDeviceType(int device_type) {
|
|
static std::map<int, DLDeviceType> dl_device_type_map{
|
|
{PROTO_CPU, kDLCPU},
|
|
{PROTO_CUDA, kDLGPU},
|
|
};
|
|
const auto it = dl_device_type_map.find(device_type);
|
|
return it == dl_device_type_map.end() ? nullptr : &it->second;
|
|
}
|
|
|
|
const DLDataType* CaffeToDLType(const TypeMeta meta) {
|
|
static std::map<TypeIdentifier, DLDataType> dl_type_map{
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<int8_t>(), DLDataType{0, 8, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<int16_t>(), DLDataType{0, 16, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<int32_t>(), DLDataType{0, 32, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<int64_t>(), DLDataType{0, 64, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<uint8_t>(), DLDataType{1, 8, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<uint16_t>(), DLDataType{1, 16, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<at::Half>(), DLDataType{2, 16, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<float>(), DLDataType{2, 32, 1}},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{TypeMeta::Id<double>(), DLDataType{2, 64, 1}},
|
|
};
|
|
const auto it = dl_type_map.find(meta.id());
|
|
return it == dl_type_map.end() ? nullptr : &it->second;
|
|
}
|
|
|
|
const TypeMeta DLTypeToCaffe(const DLDataType& dl_type) {
|
|
try {
|
|
if (dl_type.lanes != 1) {
|
|
throw std::invalid_argument("invalid type");
|
|
}
|
|
static std::map<int, std::map<int, TypeMeta>> dl_caffe_type_map{
|
|
{0,
|
|
std::map<int, TypeMeta>{
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{8, TypeMeta::Make<int8_t>()},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{16, TypeMeta::Make<int16_t>()},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{32, TypeMeta::Make<int32_t>()},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{64, TypeMeta::Make<int64_t>()},
|
|
}},
|
|
{1,
|
|
std::map<int, TypeMeta>{
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{8, TypeMeta::Make<uint8_t>()},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{16, TypeMeta::Make<uint16_t>()},
|
|
}},
|
|
{2,
|
|
std::map<int, TypeMeta>{
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{16, TypeMeta::Make<at::Half>()},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{32, TypeMeta::Make<float>()},
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
{64, TypeMeta::Make<double>()},
|
|
}},
|
|
};
|
|
if (!dl_caffe_type_map.count(dl_type.code)) {
|
|
throw std::invalid_argument("invalid type");
|
|
}
|
|
const auto& bits_map = dl_caffe_type_map.at(dl_type.code);
|
|
if (!bits_map.count(dl_type.bits)) {
|
|
throw std::invalid_argument("invalid type");
|
|
}
|
|
return bits_map.at(dl_type.bits);
|
|
} catch (const std::invalid_argument& e) {
|
|
CAFFE_THROW(
|
|
"Unsupported DLDataType: ", dl_type.code, dl_type.bits, dl_type.lanes);
|
|
}
|
|
}
|
|
|
|
} // namespace python
|
|
} // namespace caffe2
|