Skip to content

Commit cc5f8ae

Browse files
malfetpytorchmergebot
authored andcommitted
Revert D34868005: [torch::deploy] remove asserts from deploy
Test Plan: revert-hammer Differential Revision: D34868005 (pytorch@48b1654) Original commit changeset: c8bb1f7a2b16 Original Phabricator Diff: D34868005 (pytorch@48b1654) fbshipit-source-id: a9f2250880f44b2ebe09a44f955dd8568a203b1b (cherry picked from commit c0537d6)
1 parent c77857c commit cc5f8ae

12 files changed

+61
-91
lines changed

torch/csrc/deploy/Exception.h

Lines changed: 0 additions & 33 deletions
This file was deleted.

torch/csrc/deploy/deploy.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <c10/util/Exception.h>
12
#include <torch/csrc/deploy/deploy.h>
23
#include <torch/csrc/deploy/elf_file.h>
34
#include <torch/cuda.h>
@@ -45,7 +46,7 @@ const std::initializer_list<InterpreterSymbol> kInterpreterSearchPath = {
4546
};
4647

4748
static bool writeDeployInterpreter(FILE* dst) {
48-
MULTIPY_INTERNAL_ASSERT(dst);
49+
TORCH_INTERNAL_ASSERT(dst);
4950
const char* payloadStart = nullptr;
5051
size_t size = 0;
5152
bool customLoader = false;
@@ -82,7 +83,7 @@ static bool writeDeployInterpreter(FILE* dst) {
8283
payloadStart = libStart;
8384
}
8485
size_t written = fwrite(payloadStart, 1, size, dst);
85-
MULTIPY_INTERNAL_ASSERT(size == written, "expected written == size");
86+
TORCH_INTERNAL_ASSERT(size == written, "expected written == size");
8687
return customLoader;
8788
}
8889

@@ -213,9 +214,9 @@ using dlopen_t = void* (*)(const char*, int);
213214
// function.
214215
static dlopen_t find_real_dlopen() {
215216
void* libc = dlopen("libdl.so.2", RTLD_NOLOAD | RTLD_LAZY | RTLD_LOCAL);
216-
MULTIPY_INTERNAL_ASSERT(libc);
217+
TORCH_INTERNAL_ASSERT(libc);
217218
auto dlopen_ = (dlopen_t)dlsym(libc, "dlopen");
218-
MULTIPY_INTERNAL_ASSERT(dlopen_);
219+
TORCH_INTERNAL_ASSERT(dlopen_);
219220
return dlopen_;
220221
}
221222

@@ -226,7 +227,7 @@ Interpreter::Interpreter(
226227
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
227228
char libraryName[] = "/tmp/torch_deployXXXXXX";
228229
int fd = mkstemp(libraryName);
229-
MULTIPY_INTERNAL_ASSERT(fd != -1, "failed to create temporary file");
230+
TORCH_INTERNAL_ASSERT(fd != -1, "failed to create temporary file");
230231
libraryName_ = libraryName;
231232
FILE* dst = fdopen(fd, "wb");
232233

@@ -342,12 +343,12 @@ void PythonMethodWrapper::setArgumentNames(
342343
return;
343344
}
344345

345-
MULTIPY_INTERNAL_ASSERT(iArgumentNames.isList());
346+
TORCH_INTERNAL_ASSERT(iArgumentNames.isList());
346347
auto argumentNames = iArgumentNames.toListRef();
347348

348349
argumentNamesOut.reserve(argumentNames.size());
349350
for (auto& argumentName : argumentNames) {
350-
MULTIPY_INTERNAL_ASSERT(argumentName.isString());
351+
TORCH_INTERNAL_ASSERT(argumentName.isString());
351352
argumentNamesOut.push_back(argumentName.toStringRef());
352353
}
353354
}

torch/csrc/deploy/deploy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <c10/util/Optional.h>
3+
#include <c10/util/irange.h>
34
#include <torch/csrc/api/include/torch/imethod.h>
45
#include <torch/csrc/deploy/interpreter/interpreter_impl.h>
56
#include <torch/csrc/deploy/noop_environment.h>

torch/csrc/deploy/elf_file.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include <torch/csrc/deploy/Exception.h>
2-
#include <torch/csrc/deploy/elf_file.h>
31
#include <c10/util/irange.h>
2+
#include <torch/csrc/deploy/elf_file.h>
43

54
namespace torch {
65
namespace deploy {
@@ -14,7 +13,7 @@ ElfFile::ElfFile(const char* filename) : memFile_(filename) {
1413
shdrList_ = (Elf64_Shdr*)(fileData + ehdr_->e_shoff);
1514

1615
auto strtabSecNo = ehdr_->e_shstrndx;
17-
MULTIPY_CHECK(
16+
TORCH_CHECK(
1817
strtabSecNo >= 0 && strtabSecNo < numSections_,
1918
"e_shstrndx out of range");
2019

@@ -27,7 +26,7 @@ ElfFile::ElfFile(const char* filename) : memFile_(filename) {
2726
}
2827

2928
at::optional<Section> ElfFile::findSection(const char* name) const {
30-
MULTIPY_CHECK(name != nullptr, "Null name");
29+
TORCH_CHECK(name != nullptr, "Null name");
3130
at::optional<Section> found = at::nullopt;
3231
for (const auto& section : sections_) {
3332
if (strcmp(name, section.name) == 0) {
@@ -41,13 +40,13 @@ at::optional<Section> ElfFile::findSection(const char* name) const {
4140

4241
void ElfFile::checkFormat() const {
4342
// check the magic numbers
44-
MULTIPY_CHECK(
43+
TORCH_CHECK(
4544
(ehdr_->e_ident[EI_MAG0] == ELFMAG0) &&
4645
(ehdr_->e_ident[EI_MAG1] == ELFMAG1) &&
4746
(ehdr_->e_ident[EI_MAG2] == ELFMAG2) &&
4847
(ehdr_->e_ident[EI_MAG3] == ELFMAG3),
4948
"Unexpected magic numbers");
50-
MULTIPY_CHECK(
49+
TORCH_CHECK(
5150
ehdr_->e_ident[EI_CLASS] == ELFCLASS64, "Only support 64bit ELF file");
5251
}
5352

torch/csrc/deploy/elf_file.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <c10/util/Optional.h>
44
#include <elf.h>
5-
#include <torch/csrc/deploy/Exception.h>
65
#include <torch/csrc/deploy/mem_file.h>
76
#include <vector>
87

@@ -41,15 +40,15 @@ class ElfFile {
4140
const char* name = "";
4241

4342
if (strtabSection_) {
44-
MULTIPY_CHECK(nameOff >= 0 && nameOff < strtabSection_.len, "");
43+
TORCH_CHECK(nameOff >= 0 && nameOff < strtabSection_.len);
4544
name = strtabSection_.start + nameOff;
4645
}
4746
const char* start = memFile_.data() + shOff;
4847
return Section{name, start, len};
4948
}
5049

5150
[[nodiscard]] const char* str(size_t off) const {
52-
MULTIPY_CHECK(off < strtabSection_.len, "String table index out of range");
51+
TORCH_CHECK(off < strtabSection_.len, "String table index out of range");
5352
return strtabSection_.start + off;
5453
}
5554
void checkFormat() const;

torch/csrc/deploy/example/benchmark.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <ATen/ATen.h>
44
#include <ATen/TypeDefault.h>
5+
#include <c10/util/irange.h>
6+
57
#include <torch/script.h>
68

79
#include <pthread.h>

torch/csrc/deploy/interpreter/builtin_registry.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <Python.h>
2+
#include <c10/util/Exception.h>
23
#include <fmt/format.h>
3-
#include <torch/csrc/deploy/Exception.h>
44
#include <torch/csrc/deploy/interpreter/builtin_registry.h>
55

66
namespace torch {
@@ -55,10 +55,10 @@ BuiltinRegistry* BuiltinRegistry::get() {
5555
}
5656

5757
void BuiltinRegistry::runPreInitialization() {
58-
MULTIPY_INTERNAL_ASSERT(!Py_IsInitialized());
58+
TORCH_INTERNAL_ASSERT(!Py_IsInitialized());
5959
sanityCheck();
6060
PyImport_FrozenModules = BuiltinRegistry::getAllFrozenModules();
61-
MULTIPY_INTERNAL_ASSERT(PyImport_FrozenModules != nullptr);
61+
TORCH_INTERNAL_ASSERT(PyImport_FrozenModules != nullptr);
6262

6363
appendCPythonInittab();
6464
}
@@ -83,15 +83,15 @@ sys.meta_path.insert(0, F())
8383
)PYTHON";
8484

8585
void BuiltinRegistry::runPostInitialization() {
86-
MULTIPY_INTERNAL_ASSERT(Py_IsInitialized());
86+
TORCH_INTERNAL_ASSERT(Py_IsInitialized());
8787
std::string metaPathSetupScript(metaPathSetupTemplate);
8888
std::string replaceKey = "<<<DEPLOY_BUILTIN_MODULES_CSV>>>";
8989
auto itr = metaPathSetupScript.find(replaceKey);
9090
if (itr != std::string::npos) {
9191
metaPathSetupScript.replace(itr, replaceKey.size(), getBuiltinModulesCSV());
9292
}
9393
int r = PyRun_SimpleString(metaPathSetupScript.c_str());
94-
MULTIPY_INTERNAL_ASSERT(r == 0);
94+
TORCH_INTERNAL_ASSERT(r == 0);
9595
}
9696

9797
void BuiltinRegistry::registerBuiltin(
@@ -152,22 +152,22 @@ void BuiltinRegistry::sanityCheck() {
152152
auto* cpythonInternalFrozens = getItem("cpython_internal");
153153
// Num frozen builtins shouldn't change (unless modifying the underlying
154154
// cpython version)
155-
MULTIPY_INTERNAL_ASSERT(
155+
TORCH_INTERNAL_ASSERT(
156156
cpythonInternalFrozens != nullptr &&
157157
cpythonInternalFrozens->numModules == NUM_FROZEN_PY_BUILTIN_MODULES,
158158
"Missing python builtin frozen modules");
159159

160160
auto* frozenpython = getItem("frozenpython");
161161
#ifdef FBCODE_CAFFE2
162-
MULTIPY_INTERNAL_ASSERT(
162+
TORCH_INTERNAL_ASSERT(
163163
frozenpython != nullptr, "Missing frozen python modules");
164164
#else
165165
auto* frozentorch = getItem("frozentorch");
166166
// Check frozenpython+frozentorch together since in OSS frozenpython is empty
167167
// and frozentorch contains stdlib+torch, while in fbcode they are separated
168168
// due to thirdparty2 frozenpython. No fixed number of torch modules to check
169169
// for, but there should be at least one.
170-
MULTIPY_INTERNAL_ASSERT(
170+
TORCH_INTERNAL_ASSERT(
171171
frozenpython != nullptr && frozentorch != nullptr &&
172172
frozenpython->numModules + frozentorch->numModules >
173173
NUM_FROZEN_PY_STDLIB_MODULES + 1,

torch/csrc/deploy/interpreter/interpreter_impl.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
the client application.
1616
1717
It is safe to throw exception types that are defined once in
18-
the context of the client application, such as std::runtime_error,
19-
which isn't duplicated in torch::deploy interpreters.
18+
the context of the client application, such as c10::Error, which is defined
19+
in libtorch, which isn't duplicated in torch::deploy interpreters.
2020
2121
==> Use TORCH_DEPLOY_TRY, _SAFE_CATCH_RETHROW around _ALL_ torch::deploy APIs
2222
@@ -30,16 +30,20 @@
3030
3131
*/
3232
#define TORCH_DEPLOY_TRY try {
33-
#define TORCH_DEPLOY_SAFE_CATCH_RETHROW \
34-
} \
35-
catch (std::exception & err) { \
36-
throw std::runtime_error( \
37-
"Exception Caught inside torch::deploy embedded library: \n" + \
38-
std::string(err.what())); \
39-
} \
40-
catch (...) { \
41-
throw std::runtime_error( \
42-
"Unknown Exception Caught inside torch::deploy embedded library"); \
33+
#define TORCH_DEPLOY_SAFE_CATCH_RETHROW \
34+
} \
35+
catch (std::exception & err) { \
36+
throw c10::Error( \
37+
std::string( \
38+
"Exception Caught inside torch::deploy embedded library: \n") + \
39+
err.what(), \
40+
""); \
41+
} \
42+
catch (...) { \
43+
throw c10::Error( \
44+
std::string( \
45+
"Unknown Exception Caught inside torch::deploy embedded library"), \
46+
""); \
4347
}
4448
namespace torch {
4549
namespace deploy {

torch/csrc/deploy/mem_file.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

3+
#include <c10/util/Exception.h>
34
#include <fcntl.h>
45
#include <sys/mman.h>
56
#include <sys/stat.h>
6-
#include <torch/csrc/deploy/Exception.h>
77
#include <unistd.h>
88
#include <cerrno>
99
#include <cstdio>
@@ -20,21 +20,18 @@ namespace deploy {
2020
struct MemFile {
2121
explicit MemFile(const char* filename_) : fd_(0), mem_(nullptr), n_bytes_(0) {
2222
fd_ = open(filename_, O_RDONLY);
23-
MULTIPY_CHECK(
24-
fd_ != -1, "failed to open " + filename_ + ": " + strerror(errno));
23+
TORCH_CHECK(fd_ != -1, "failed to open {}: {}", filename_, strerror(errno));
2524
// NOLINTNEXTLINE
2625
struct stat s;
2726
if (-1 == fstat(fd_, &s)) {
2827
close(fd_); // destructors don't run during exceptions
29-
MULTIPY_CHECK(
30-
false, "failed to stat " + filename_ + ": " + strerror(errno));
28+
TORCH_CHECK(false, "failed to stat {}: {}", filename_, strerror(errno));
3129
}
3230
n_bytes_ = s.st_size;
3331
mem_ = mmap(nullptr, n_bytes_, PROT_READ, MAP_SHARED, fd_, 0);
3432
if (MAP_FAILED == mem_) {
3533
close(fd_);
36-
MULTIPY_CHECK(
37-
false, "failed to mmap " + filename_ + ": " + strerror(errno));
34+
TORCH_CHECK(false, "failed to mmap {}: {}", filename_, strerror(errno));
3835
}
3936
}
4037
MemFile(const MemFile&) = delete;

0 commit comments

Comments
 (0)