LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
transform.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_TRANSFORMS_TRANSFORM_HPP_INCLUDED
28 #define LBANN_TRANSFORMS_TRANSFORM_HPP_INCLUDED
29 
30 #include "lbann/base.hpp"
33 #include "lbann/utils/random.hpp"
35 
36 namespace lbann {
37 namespace transform {
38 
50 class transform
51 {
52 public:
53  transform() = default;
54  transform(const transform&) = default;
55  transform& operator=(const transform&) = default;
56  virtual ~transform() = default;
57 
59  virtual transform* copy() const = 0;
60 
62  virtual std::string get_type() const = 0;
64  virtual description get_description() const
65  {
66  return description(get_type() + " transform");
67  }
68 
70  virtual bool supports_non_inplace() const { return false; }
71 
81  virtual void apply(utils::type_erased_matrix& data,
82  std::vector<size_t>& dims) = 0;
83 
88  virtual void
89  apply(utils::type_erased_matrix& data, CPUMat& out, std::vector<size_t>& dims)
90  {
91  LBANN_ERROR("Non-in-place apply not implemented.");
92  }
93 
94 protected:
96  static inline float get_uniform_random(float a, float b)
97  {
99  std::uniform_real_distribution<float> dist(a, b);
100  return dist(gen);
101  }
103  static inline bool get_bool_random(float p)
104  {
105  return get_uniform_random(0.0, 1.0) < p;
106  }
108  static inline El::Int get_uniform_random_int(El::Int a, El::Int b)
109  {
111  return fast_rand_int(gen, b - a) + a;
112  }
113 };
114 
115 } // namespace transform
116 } // namespace lbann
117 
118 #endif // LBANN_TRANSFORMS_TRANSFORM_HPP_INCLUDED
T fast_rand_int(Generator &g, T max)
Definition: random.hpp:56
virtual description get_description() const
Definition: transform.hpp:64
virtual void apply(utils::type_erased_matrix &data, CPUMat &out, std::vector< size_t > &dims)
Definition: transform.hpp:89
static float get_uniform_random(float a, float b)
Definition: transform.hpp:96
#define LBANN_ERROR(...)
Definition: exception.hpp:37
static bool get_bool_random(float p)
Definition: transform.hpp:103
Generates nicely formatted description messages.
Definition: description.hpp:49
virtual void apply(utils::type_erased_matrix &data, std::vector< size_t > &dims)=0
std::minstd_rand fast_rng_gen
T & data(const cnpy::NpyArray &na, const std::vector< size_t > indices)
Definition: cnpy_utils.hpp:75
virtual bool supports_non_inplace() const
Definition: transform.hpp:70
virtual std::string get_type() const =0
transform & operator=(const transform &)=default
virtual ~transform()=default
El::Matrix< DataType, El::Device::CPU > CPUMat
Definition: base.hpp:116
virtual transform * copy() const =0
A type-erased wrapper around an El::Matrix<T,Device::CPU>
fast_rng_gen & get_fast_io_generator()
static El::Int get_uniform_random_int(El::Int a, El::Int b)
Definition: transform.hpp:108