LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
data_reader_python.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_DATA_READERS_PYTHON_HPP_INCLUDED
28 #define LBANN_DATA_READERS_PYTHON_HPP_INCLUDED
29 
30 #include "data_reader.hpp"
31 #ifdef LBANN_HAS_EMBEDDED_PYTHON
32 #include "lbann/utils/python.hpp"
33 
34 namespace lbann {
35 
36 class python_reader : public generic_data_reader
37 {
38 public:
39  python_reader(std::string module,
40  std::string module_dir,
41  std::string sample_function,
42  std::string num_samples_function,
43  std::string sample_dims_function,
44  bool shuffle);
45  python_reader(const python_reader&) = default;
46  python_reader& operator=(const python_reader&) = default;
47  ~python_reader() override;
48  python_reader* copy() const override { return new python_reader(*this); }
49 
50  std::string get_type() const override { return "python_reader"; }
51 
52  const std::vector<El::Int> get_data_dims() const override;
53  int get_num_labels() const override;
54  int get_linearized_data_size() const override;
55  int get_linearized_label_size() const override;
56 
57  void setup(int num_io_threads,
58  observer_ptr<thread_pool> io_thread_pool) override;
59  void load() override;
60 
61 protected:
62  bool fetch_data_block(std::map<data_field_type, CPUMat*>& input_buffers,
63  El::Int block_offset,
64  El::Int block_stride,
65  El::Int mb_size,
66  El::Matrix<El::Int>& indices_fetched) override;
67  bool fetch_label(CPUMat& Y, int data_id, int mb_idx) override;
68 
69 private:
71  std::vector<El::Int> m_sample_dims;
73  El::Int m_num_samples;
74 
81  python::object m_sample_function;
82 
89  python::object m_sample_function_wrapper;
90 
95  python::object m_process_pool;
96 
101  python::object m_shared_memory_array;
102 
107  DataType* m_shared_memory_array_ptr = nullptr;
108 };
109 
110 } // namespace lbann
111 
112 #endif // LBANN_HAS_EMBEDDED_PYTHON
113 #endif // LBANN_DATA_READERS_PYTHON_HPP_INCLUDED
void load(std::string const &pbuf_filename, google::protobuf::Message &msg)
Fill the protobuf message from a binary file.
El::Matrix< DataType, El::Device::CPU > CPUMat
Definition: base.hpp:116