pytorch/test/cpp/api/support.h
Will Feng 085bd15880 Add TORCH_WARN_ONCE, and use it in Tensor.data<T>() (#25207)
Summary:
This PR adds `TORCH_WARN_ONCE` macro, and use it in `Tensor.data<T>()`.

cc. gchanan
Pull Request resolved: https://github.com/pytorch/pytorch/pull/25207

Differential Revision: D17066263

Pulled By: yf225

fbshipit-source-id: 411c6ccc8326fb27ff885fee4638df8b5ba4d449
2019-08-27 21:42:44 -07:00

66 lines
1.4 KiB
C++

#pragma once
#include <test/cpp/common/support.h>
#include <gtest/gtest.h>
#include <torch/nn/cloneable.h>
#include <torch/types.h>
#include <torch/utils.h>
#include <string>
#include <utility>
namespace torch {
namespace test {
// Lets you use a container without making a new class,
// for experimental implementations
class SimpleContainer : public nn::Cloneable<SimpleContainer> {
public:
void reset() override {}
template <typename ModuleHolder>
ModuleHolder add(
ModuleHolder module_holder,
std::string name = std::string()) {
return Module::register_module(std::move(name), module_holder);
}
};
struct SeedingFixture : public ::testing::Test {
SeedingFixture() {
torch::manual_seed(0);
}
};
struct CerrRedirect {
CerrRedirect(std::streambuf * new_buffer) : prev_buffer(std::cerr.rdbuf(new_buffer)) {}
~CerrRedirect( ) {
std::cerr.rdbuf(prev_buffer);
}
private:
std::streambuf * prev_buffer;
};
inline bool pointer_equal(at::Tensor first, at::Tensor second) {
return first.data_ptr<float>() == second.data_ptr<float>();
}
inline int count_substr_occurrences(const std::string& str, const std::string& substr) {
int count = 0;
size_t pos = str.find(substr);
while (pos != std::string::npos) {
count++;
pos = str.find(substr, pos + substr.size());
}
return count;
}
} // namespace test
} // namespace torch