mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/22505 Differential Revision: D16119310 Pulled By: Krovatkin fbshipit-source-id: 8b742411f40d66690ce28726c213741e0c2de618
58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <ATen/core/jit_type.h>
|
|
#include <torch/csrc/jit/ir.h>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
namespace script {
|
|
|
|
// Simple data structure for containing a type T in nested control blocks
|
|
// Should only be used after initial compilation where type checking and
|
|
// loads and stores are emitted
|
|
|
|
template <typename T>
|
|
struct MiniEnvironment {
|
|
MiniEnvironment(Block* b, std::shared_ptr<MiniEnvironment> next = nullptr)
|
|
: next(std::move(next)) {}
|
|
|
|
std::shared_ptr<MiniEnvironment<T>> next;
|
|
|
|
T findInThisFrame(const std::string& name) {
|
|
auto it = table.find(name);
|
|
if (it != table.end()) {
|
|
return it->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
T findInAnyFrame(const std::string& name) {
|
|
for (auto runner = this; runner; runner = runner->next.get()) {
|
|
if (auto r = runner->findInThisFrame(name)) {
|
|
return r;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void setVar(const std::string& name, T value) {
|
|
table[name] = value;
|
|
}
|
|
|
|
std::vector<std::string> definedVariables() {
|
|
std::vector<std::string> result;
|
|
for (auto& kv : table) {
|
|
result.push_back(kv.first);
|
|
}
|
|
std::sort(result.begin(), result.end());
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
std::unordered_map<std::string, T> table;
|
|
};
|
|
|
|
} // namespace script
|
|
} // namespace jit
|
|
} // namespace torch
|