23 {
"init_capacity",
"Initial capacity of a hash container."},
24 {
"key_dtype",
"Data type for the input key tensor."},
26 "Element shape for the input key tensor. E.g. (3) for 3D "
28 {
"value_dtype",
"Data type for the input value tensor."},
29 {
"value_dtypes",
"List of data type for the input value tensors."},
30 {
"value_element_shape",
31 "Element shape for the input value tensor. E.g. (1) for mapped "
33 {
"value_element_shapes",
34 "List of element shapes for the input value tensors. E.g. ((8,8,8,1), "
36 "mapped weights and RGB colors stored in 8^3 element arrays."},
38 "Compute device to store and operate on the hash container."},
40 "If true, a new tensor is always created; if false, the copy is "
41 "avoided when the original tensor already has the targeted dtype."},
43 "Input keys stored in a tensor of shape (N, key_element_shape)."},
45 "Input values stored in a tensor of shape (N, value_element_shape)."},
47 "List of input values stored in tensors of corresponding shapes."},
48 {
"capacity",
"New capacity for rehashing."},
49 {
"file_name",
"File name of the corresponding .npz file."},
50 {
"values_buffer_id",
"Index of the value buffer tensor."},
51 {
"device_id",
"Target CUDA device ID."}};
55 py::native_enum<HashBackendType>(m,
"HashBackendType",
"enum.Enum")
62 py::class_<HashMap> hashmap(
64 "A HashMap is a map from key to data wrapped by Tensors.");
68 "init_capacity"_a,
"key_dtype"_a,
"key_element_shape"_a,
69 "value_dtype"_a,
"value_element_shape"_a,
70 "device"_a =
Device(
"CPU:0"));
72 const std::vector<Dtype>&,
73 const std::vector<SizeVector>&,
const Device&>(),
74 "init_capacity"_a,
"key_dtype"_a,
"key_element_shape"_a,
75 "value_dtypes"_a,
"value_element_shapes"_a,
76 "device"_a =
Device(
"CPU:0"));
83 h.
Insert(keys, values, buf_indices, masks);
84 return py::make_tuple(buf_indices, masks);
86 "Insert an array of keys and an array of values stored in Tensors.",
87 "keys"_a,
"values"_a);
91 const std::vector<Tensor>& values) {
93 h.
Insert(keys, values, buf_indices, masks);
94 return py::make_tuple(buf_indices, masks);
96 "Insert an array of keys and a list of value arrays stored in "
98 "keys"_a,
"list_values"_a);
104 Tensor buf_indices, masks;
105 h.
Activate(keys, buf_indices, masks);
106 return py::make_tuple(buf_indices, masks);
108 "Activate an array of keys stored in Tensors without copying "
116 Tensor buf_indices, masks;
117 h.
Find(keys, buf_indices, masks);
118 return py::make_tuple(buf_indices, masks);
120 "Find an array of keys stored in Tensors.",
"keys"_a);
127 h.
Erase(keys, masks);
130 "Erase an array of keys stored in Tensors.",
"keys"_a);
134 "active_buf_indices",
140 "Get the buffer indices corresponding to active entries in the "
143 hashmap.def(
"save", &
HashMap::Save,
"Save the hash map into a .npz file.",
148 "Load a hash map from a .npz file.",
"file_name"_a);
152 "Reserve the hash map given the capacity.",
"capacity"_a);
156 "Get the key tensor stored in the buffer.");
158 "Get the list of value tensors stored in the buffer.");
161 "Get the value tensor stored at index 0.");
165 "Get the value tensor stored at index i",
"value_buffer_id"_a);
169 hashmap.def(
"size", &
HashMap::Size,
"Get the size of the hash map.");
171 "Get the capacity of the hash map.");
174 "Clone the hash map, including the data structure and the data "
177 "Convert the hash map to a selected device.",
"device"_a,
186 "Transfer the hash map to CPU. If the hash map "
187 "is already on CPU, no copy will be performed.");
190 [](
const HashMap& hashmap,
int device_id) {
193 "Transfer the hash map to a CUDA device. If the hash map is "
195 "on the specified CUDA device, no copy will be performed.",
205 py::class_<HashSet> hashset(
207 "A HashSet is an unordered set of keys wrapped by Tensors.");
209 py::init<int64_t, const Dtype&, const SizeVector&, const Device&>(),
210 "init_capacity"_a,
"key_dtype"_a,
"key_element_shape"_a,
211 "device"_a =
Device(
"CPU:0"));
217 Tensor buf_indices, masks;
218 h.
Insert(keys, buf_indices, masks);
219 return py::make_tuple(buf_indices, masks);
221 "Insert an array of keys stored in Tensors.",
"keys"_a);
227 Tensor buf_indices, masks;
228 h.
Find(keys, buf_indices, masks);
229 return py::make_tuple(buf_indices, masks);
231 "Find an array of keys stored in Tensors.",
"keys"_a);
238 h.
Erase(keys, masks);
241 "Erase an array of keys stored in Tensors.",
"keys"_a);
245 "active_buf_indices",
251 "Get the buffer indices corresponding to active entries in the "
254 hashset.def(
"save", &
HashSet::Save,
"Save the hash set into a .npz file.",
259 "Load a hash set from a .npz file.",
"file_name"_a);
263 "Reserve the hash set given the capacity.",
"capacity"_a);
267 "Get the key tensor stored in the buffer.");
269 hashset.def(
"size", &
HashSet::Size,
"Get the size of the hash set.");
271 "Get the capacity of the hash set.");
274 "Clone the hash set, including the data structure and the data "
277 "Convert the hash set to a selected device.",
"device"_a,
286 "Transfer the hash set to CPU. If the hash set "
287 "is already on CPU, no copy will be performed.");
290 [](
const HashSet& hashset,
int device_id) {
293 "Transfer the hash set to a CUDA device. If the hash set is "
295 "on the specified CUDA device, no copy will be performed.",
std::pair< Tensor, Tensor > Activate(const Tensor &input_keys)
std::pair< Tensor, Tensor > Find(const Tensor &input_keys)
HashMap To(const Device &device, bool copy=false) const
Convert the hash map to another device.
std::vector< Tensor > GetValueTensors() const
Tensor Erase(const Tensor &input_keys)
static HashMap Load(const std::string &file_name)
void Save(const std::string &file_name)
int64_t GetCapacity() const
Get the capacity of the hash map.
std::pair< Tensor, Tensor > Insert(const Tensor &input_keys, const Tensor &input_values)
void Reserve(int64_t capacity)
Reserve the internal hash map with the given capacity by rehashing.
Tensor GetActiveIndices() const
Tensor GetKeyTensor() const
Device GetDevice() const override
Get the device of the hash map.
int64_t Size() const
Get the size (number of active entries) of the hash map.
Tensor GetValueTensor(size_t index=0) const
HashMap Clone() const
Clone the hash map with buffers.
void Save(const std::string &file_name)
std::pair< Tensor, Tensor > Find(const Tensor &input_keys)
int64_t Size() const
Get the size (number of active entries) of the hash set.
Tensor GetKeyTensor() const
static HashSet Load(const std::string &file_name)
Load active keys and values from a npz file that contains 'key'.
void Reserve(int64_t capacity)
Reserve the internal hash map with the capcity by rehashing.
int64_t GetCapacity() const
Get the capacity of the hash set.
Tensor GetActiveIndices() const
std::pair< Tensor, Tensor > Insert(const Tensor &input_keys)
HashSet To(const Device &device, bool copy=false) const
Convert the hash set to another device.
HashSet Clone() const
Clone the hash set with buffers.
Tensor Erase(const Tensor &input_keys)
Device GetDevice() const override
Get the device of the hash set.
void pybind_core_hashmap(py::module &m)
const std::unordered_map< std::string, std::string > argument_docs
void pybind_core_hashset(py::module &m)
void ClassMethodDocInject(py::module &pybind_module, const std::string &class_name, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Generic file read and write utility for python interface.