Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader.h +57 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader_options.h +65 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/datasets.h +9 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/detail/data_shuttle.h +87 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/detail/queue.h +84 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/detail/sequencers.h +113 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/example.h +55 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/iterator.h +178 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/samplers.h +9 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/samplers/custom_batch_request.h +21 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/samplers/random.h +54 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms.h +7 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms/collate.h +35 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms/lambda.h +56 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms/stack.h +49 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/worker_exception.h +38 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/activation.h +966 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/batchnorm.h +83 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/conv.h +301 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/distance.h +88 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/dropout.h +234 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/embedding.h +211 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/fold.h +102 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/instancenorm.h +63 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/linear.h +37 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/loss.h +1044 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/normalization.h +211 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/padding.h +58 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/pixelshuffle.h +47 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/pooling.h +1098 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/upsampling.h +290 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/vision.h +124 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/_functions.h +26 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/activation.h +875 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/adaptive.h +109 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/batchnorm.h +250 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/any.h +372 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/any_module_holder.h +133 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/any_value.h +124 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/functional.h +105 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/moduledict.h +262 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/modulelist.h +274 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/named_any.h +94 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/parameterdict.h +148 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/parameterlist.h +169 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/sequential.h +390 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/distance.h +86 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/embedding.h +171 -0
- videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/fold.h +87 -0
.gitattributes
CHANGED
|
@@ -1722,3 +1722,4 @@ parrot/lib/python3.10/site-packages/scipy/linalg/_decomp_update.cpython-310-x86_
|
|
| 1722 |
parrot/lib/python3.10/site-packages/scipy/optimize/__pycache__/_optimize.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1723 |
vllm/lib/python3.10/site-packages/cupy/lib/_polynomial.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1724 |
parrot/lib/python3.10/site-packages/scipy/optimize/_cobyla.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 1722 |
parrot/lib/python3.10/site-packages/scipy/optimize/__pycache__/_optimize.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1723 |
vllm/lib/python3.10/site-packages/cupy/lib/_polynomial.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1724 |
parrot/lib/python3.10/site-packages/scipy/optimize/_cobyla.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1725 |
+
vllm/lib/python3.10/site-packages/cupy/_core/_optimize_config.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader.h
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/dataloader/stateful.h>
|
| 4 |
+
#include <torch/data/dataloader/stateless.h>
|
| 5 |
+
|
| 6 |
+
#include <torch/csrc/utils/variadic.h>
|
| 7 |
+
|
| 8 |
+
#include <c10/util/Exception.h>
|
| 9 |
+
|
| 10 |
+
#include <cstddef>
|
| 11 |
+
#include <memory>
|
| 12 |
+
#include <type_traits>
|
| 13 |
+
#include <utility>
|
| 14 |
+
|
| 15 |
+
namespace torch {
|
| 16 |
+
namespace data {
|
| 17 |
+
|
| 18 |
+
/// Creates a `DataLoader` instance for a stateless `dataset`, a `sampler` and
|
| 19 |
+
/// some `options`.
|
| 20 |
+
template <typename Dataset, typename Sampler>
|
| 21 |
+
torch::disable_if_t<
|
| 22 |
+
Dataset::is_stateful,
|
| 23 |
+
std::unique_ptr<StatelessDataLoader<Dataset, Sampler>>>
|
| 24 |
+
make_data_loader(Dataset dataset, Sampler sampler, DataLoaderOptions options) {
|
| 25 |
+
return std::make_unique<StatelessDataLoader<Dataset, Sampler>>(
|
| 26 |
+
std::move(dataset), std::move(sampler), std::move(options));
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
/// Creates a `DataLoader` instance for a stateless `dataset` and some
|
| 30 |
+
/// `options`. A sampler (by default a `RandomSampler`) will be constructed from
|
| 31 |
+
/// the size of the dataset.
|
| 32 |
+
template <typename Sampler = samplers::RandomSampler, typename Dataset>
|
| 33 |
+
torch::disable_if_t<
|
| 34 |
+
Dataset::is_stateful || !std::is_constructible<Sampler, size_t>::value,
|
| 35 |
+
std::unique_ptr<StatelessDataLoader<Dataset, Sampler>>>
|
| 36 |
+
make_data_loader(
|
| 37 |
+
Dataset dataset,
|
| 38 |
+
DataLoaderOptions options = DataLoaderOptions()) {
|
| 39 |
+
const optional<size_t> size = dataset.size();
|
| 40 |
+
TORCH_CHECK(
|
| 41 |
+
size.has_value(),
|
| 42 |
+
"Expected the dataset to be sized in "
|
| 43 |
+
"order to construct the Sampler");
|
| 44 |
+
return make_data_loader(
|
| 45 |
+
std::move(dataset), Sampler(*size), std::move(options));
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
/// Creates a `DataLoader` for a stateful `dataset` and some `options`.
|
| 49 |
+
template <typename Dataset, typename = torch::enable_if_t<Dataset::is_stateful>>
|
| 50 |
+
std::unique_ptr<StatefulDataLoader<Dataset>> make_data_loader(
|
| 51 |
+
Dataset dataset,
|
| 52 |
+
DataLoaderOptions options = DataLoaderOptions()) {
|
| 53 |
+
return std::make_unique<StatefulDataLoader<Dataset>>(
|
| 54 |
+
std::move(dataset), std::move(options));
|
| 55 |
+
}
|
| 56 |
+
} // namespace data
|
| 57 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/dataloader_options.h
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/arg.h>
|
| 4 |
+
#include <torch/types.h>
|
| 5 |
+
|
| 6 |
+
#include <chrono>
|
| 7 |
+
#include <cstddef>
|
| 8 |
+
|
| 9 |
+
namespace torch {
|
| 10 |
+
namespace data {
|
| 11 |
+
|
| 12 |
+
/// Options to configure a `DataLoader`.
|
| 13 |
+
struct DataLoaderOptions {
|
| 14 |
+
DataLoaderOptions() = default;
|
| 15 |
+
/* implicit */ DataLoaderOptions(size_t batch_size)
|
| 16 |
+
: batch_size_(batch_size) {}
|
| 17 |
+
|
| 18 |
+
/// The size of each batch to fetch.
|
| 19 |
+
TORCH_ARG(size_t, batch_size) = 1;
|
| 20 |
+
|
| 21 |
+
/// The number of worker threads to launch. If zero, the main thread will
|
| 22 |
+
/// synchronously perform the data loading.
|
| 23 |
+
TORCH_ARG(size_t, workers) = 0;
|
| 24 |
+
|
| 25 |
+
/// The maximum number of jobs to enqueue for fetching by worker threads.
|
| 26 |
+
/// Defaults to two times the number of worker threads.
|
| 27 |
+
TORCH_ARG(optional<size_t>, max_jobs);
|
| 28 |
+
|
| 29 |
+
/// An optional limit on the time to wait for the next batch.
|
| 30 |
+
TORCH_ARG(optional<std::chrono::milliseconds>, timeout);
|
| 31 |
+
|
| 32 |
+
/// Whether to enforce ordering of batches when multiple are loaded
|
| 33 |
+
/// asynchronously by worker threads. Set to `false` for better performance if
|
| 34 |
+
/// you do not care about determinism.
|
| 35 |
+
TORCH_ARG(bool, enforce_ordering) = true;
|
| 36 |
+
|
| 37 |
+
/// Whether to omit the last batch if it contains less than `batch_size`
|
| 38 |
+
/// examples.
|
| 39 |
+
TORCH_ARG(bool, drop_last) = false;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
/// Like `DataLoaderOptions`, but without any unconfigured state.
|
| 43 |
+
/// `DataLoaderOptions` has some options that depend on other options
|
| 44 |
+
/// (`max_jobs` => `2 * workers`). In the spirit of properly using the C++ type
|
| 45 |
+
/// system, `DataLoaderOptions` allows only setting values. To access values,
|
| 46 |
+
/// you must create a `FullDataLoaderOptions` from a `DataLoaderOptions`
|
| 47 |
+
/// instance, which will do any necessary coalescing.
|
| 48 |
+
struct FullDataLoaderOptions {
|
| 49 |
+
explicit FullDataLoaderOptions(DataLoaderOptions options)
|
| 50 |
+
: batch_size(options.batch_size()),
|
| 51 |
+
workers(options.workers()),
|
| 52 |
+
max_jobs(options.max_jobs().value_or(2 * workers)),
|
| 53 |
+
timeout(options.timeout()),
|
| 54 |
+
enforce_ordering(options.enforce_ordering()),
|
| 55 |
+
drop_last(options.drop_last()) {}
|
| 56 |
+
|
| 57 |
+
size_t batch_size;
|
| 58 |
+
size_t workers;
|
| 59 |
+
size_t max_jobs;
|
| 60 |
+
optional<std::chrono::milliseconds> timeout;
|
| 61 |
+
bool enforce_ordering;
|
| 62 |
+
bool drop_last;
|
| 63 |
+
};
|
| 64 |
+
} // namespace data
|
| 65 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/datasets.h
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/datasets/base.h>
|
| 4 |
+
#include <torch/data/datasets/chunk.h>
|
| 5 |
+
#include <torch/data/datasets/map.h>
|
| 6 |
+
#include <torch/data/datasets/mnist.h>
|
| 7 |
+
#include <torch/data/datasets/shared.h>
|
| 8 |
+
#include <torch/data/datasets/stateful.h>
|
| 9 |
+
#include <torch/data/datasets/tensor.h>
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/detail/data_shuttle.h
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/detail/queue.h>
|
| 4 |
+
#include <torch/types.h>
|
| 5 |
+
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
#include <c10/util/Optional.h>
|
| 8 |
+
|
| 9 |
+
#include <chrono>
|
| 10 |
+
#include <utility>
|
| 11 |
+
|
| 12 |
+
namespace torch {
|
| 13 |
+
namespace data {
|
| 14 |
+
namespace detail {
|
| 15 |
+
|
| 16 |
+
/// Encapsulates the full life cycle of DataLoader jobs.
|
| 17 |
+
///
|
| 18 |
+
/// When a new job is enqueued to the `DataShuttle`, a counter for in-flight
|
| 19 |
+
/// jobs is bumped. This job is said to be "in-flight" until its result is
|
| 20 |
+
/// popped. Worker threads dequeue jobs as soon as they are available. When a
|
| 21 |
+
/// worker finishes a job, it enqueues the result. Only when the main thread
|
| 22 |
+
/// dequeues a result is the count of in-flight jobs decremented. When the main
|
| 23 |
+
/// thread attempts to dequeue a job but no jobs are in-flight, that means the
|
| 24 |
+
/// epoch is complete and `pop_result` returns an empty optional.
|
| 25 |
+
template <typename Job, typename Result>
|
| 26 |
+
class DataShuttle {
|
| 27 |
+
public:
|
| 28 |
+
/// Pushes a new job. Called by the main thread.
|
| 29 |
+
void push_job(Job job) {
|
| 30 |
+
new_jobs_.push(std::move(job));
|
| 31 |
+
++in_flight_jobs_;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/// Pushes the result of a job. Called by worker threads.
|
| 35 |
+
void push_result(Result result) {
|
| 36 |
+
results_.push(std::move(result));
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/// Returns the next job, blocking until there is one available. Called by
|
| 40 |
+
/// worker threads.
|
| 41 |
+
Job pop_job() {
|
| 42 |
+
return new_jobs_.pop();
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
/// Returns the result of a job, or nullopt if all jobs were exhausted. Called
|
| 46 |
+
/// by the main thread.
|
| 47 |
+
optional<Result> pop_result(
|
| 48 |
+
optional<std::chrono::milliseconds> timeout = nullopt) {
|
| 49 |
+
if (in_flight_jobs_ > 0) {
|
| 50 |
+
auto result = results_.pop(timeout);
|
| 51 |
+
--in_flight_jobs_;
|
| 52 |
+
return result;
|
| 53 |
+
}
|
| 54 |
+
return nullopt;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/// Discards any jobs that are not yet in flight, and waits for all in-flight
|
| 58 |
+
/// jobs to finish, discarding their result.
|
| 59 |
+
void drain() {
|
| 60 |
+
// Clear all inputs so that no further jobs are scheduled.
|
| 61 |
+
auto number_cleared = new_jobs_.clear();
|
| 62 |
+
in_flight_jobs_ -= number_cleared;
|
| 63 |
+
// Remove any outstanding results.
|
| 64 |
+
while (in_flight_jobs_ > 0) {
|
| 65 |
+
pop_result();
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
/// Returns the number of jobs that are still in progress.
|
| 70 |
+
/// When this number is zero, an epoch is finished.
|
| 71 |
+
size_t in_flight_jobs() const noexcept {
|
| 72 |
+
return in_flight_jobs_;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
private:
|
| 76 |
+
/// The queue for jobs that are not yet in flight.
|
| 77 |
+
Queue<Job> new_jobs_;
|
| 78 |
+
/// The number of in-flight jobs.
|
| 79 |
+
/// NOTE: Not atomic because only manipulated by the main thread.
|
| 80 |
+
size_t in_flight_jobs_ = 0;
|
| 81 |
+
/// The queue for results of finished jobs.
|
| 82 |
+
Queue<Result> results_;
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
} // namespace detail
|
| 86 |
+
} // namespace data
|
| 87 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/detail/queue.h
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/types.h>
|
| 4 |
+
|
| 5 |
+
#include <c10/util/Exception.h>
|
| 6 |
+
|
| 7 |
+
#include <chrono>
|
| 8 |
+
#include <condition_variable>
|
| 9 |
+
#include <cstddef>
|
| 10 |
+
#include <mutex>
|
| 11 |
+
#include <queue>
|
| 12 |
+
|
| 13 |
+
namespace torch {
|
| 14 |
+
namespace data {
|
| 15 |
+
namespace detail {
|
| 16 |
+
|
| 17 |
+
/// A basic locked, blocking MPMC queue.
|
| 18 |
+
///
|
| 19 |
+
/// Every `push` and `pop` is guarded by a mutex. A condition variable is used
|
| 20 |
+
/// to communicate insertion of new elements, such that waiting threads will be
|
| 21 |
+
/// woken up if they are currently waiting inside a call to `pop()`.
|
| 22 |
+
///
|
| 23 |
+
/// Note that this data structure is written specifically for use with the
|
| 24 |
+
/// `DataLoader`. Its behavior is tailored to this use case and may not be
|
| 25 |
+
/// applicable to more general uses.
|
| 26 |
+
template <typename T>
|
| 27 |
+
class Queue {
|
| 28 |
+
public:
|
| 29 |
+
/// Pushes a new value to the back of the `Queue` and notifies one thread on
|
| 30 |
+
/// the waiting side about this event.
|
| 31 |
+
void push(T value) {
|
| 32 |
+
{
|
| 33 |
+
std::lock_guard<std::mutex> lock(mutex_);
|
| 34 |
+
queue_.push(std::move(value));
|
| 35 |
+
}
|
| 36 |
+
cv_.notify_one();
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/// Blocks until at least one element is ready to be popped from the front of
|
| 40 |
+
/// the queue. An optional `timeout` in seconds can be used to limit the time
|
| 41 |
+
/// spent waiting for an element. If the wait times out, an exception is
|
| 42 |
+
/// raised.
|
| 43 |
+
T pop(optional<std::chrono::milliseconds> timeout = nullopt) {
|
| 44 |
+
std::unique_lock<std::mutex> lock(mutex_);
|
| 45 |
+
if (timeout) {
|
| 46 |
+
if (!cv_.wait_for(
|
| 47 |
+
lock, *timeout, [this] { return !this->queue_.empty(); })) {
|
| 48 |
+
// clang-format off
|
| 49 |
+
AT_ERROR(
|
| 50 |
+
"Timeout in DataLoader queue while waiting for next batch"
|
| 51 |
+
" (timeout was ", timeout->count(), " ms)");
|
| 52 |
+
// clang-format on
|
| 53 |
+
}
|
| 54 |
+
} else {
|
| 55 |
+
cv_.wait(lock, [this] { return !this->queue_.empty(); });
|
| 56 |
+
}
|
| 57 |
+
AT_ASSERT(!queue_.empty());
|
| 58 |
+
T value = queue_.front();
|
| 59 |
+
queue_.pop();
|
| 60 |
+
lock.unlock();
|
| 61 |
+
return value;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/// Empties the queue and returns the number of elements that were present at
|
| 65 |
+
/// the start of the function. No threads are notified about this event as it
|
| 66 |
+
/// is assumed to be used to drain the queue during shutdown of a
|
| 67 |
+
/// `DataLoader`.
|
| 68 |
+
size_t clear() {
|
| 69 |
+
std::lock_guard<std::mutex> lock(this->mutex_);
|
| 70 |
+
const auto size = queue_.size();
|
| 71 |
+
while (!queue_.empty()) {
|
| 72 |
+
queue_.pop();
|
| 73 |
+
}
|
| 74 |
+
return size;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
private:
|
| 78 |
+
std::queue<T> queue_;
|
| 79 |
+
std::mutex mutex_;
|
| 80 |
+
std::condition_variable cv_;
|
| 81 |
+
};
|
| 82 |
+
} // namespace detail
|
| 83 |
+
} // namespace data
|
| 84 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/detail/sequencers.h
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/types.h>
|
| 4 |
+
|
| 5 |
+
#include <algorithm>
|
| 6 |
+
#include <cstddef>
|
| 7 |
+
#include <vector>
|
| 8 |
+
|
| 9 |
+
namespace torch {
|
| 10 |
+
namespace data {
|
| 11 |
+
namespace detail {
|
| 12 |
+
namespace sequencers {
|
| 13 |
+
namespace detail {
|
| 14 |
+
template <typename Result>
|
| 15 |
+
bool buffer_contains_result(const std::vector<optional<Result>>& buffer) {
|
| 16 |
+
return std::any_of(
|
| 17 |
+
buffer.begin(), buffer.end(), [](const optional<Result>& result) {
|
| 18 |
+
return result.has_value();
|
| 19 |
+
});
|
| 20 |
+
}
|
| 21 |
+
} // namespace detail
|
| 22 |
+
|
| 23 |
+
/// A `Sequencer` accepts a function that yields the next result of a
|
| 24 |
+
/// `DataLoader` and then has the opportunity to influence the order in which
|
| 25 |
+
/// these results are returned. The `NoSequencer` does not enforce any
|
| 26 |
+
/// sequencing and returns any result directly. The `OrderedSequencer` instead
|
| 27 |
+
/// buffers results internally to return them in order of their sequence number.
|
| 28 |
+
template <typename Result>
|
| 29 |
+
struct Sequencer {
|
| 30 |
+
using ResultProducer = std::function<optional<Result>()>;
|
| 31 |
+
virtual ~Sequencer() = default;
|
| 32 |
+
virtual optional<Result> next(ResultProducer next_result) = 0;
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
/// A `Sequencer` that does not enforce any ordering. It is effectively the
|
| 36 |
+
/// identity function.
|
| 37 |
+
template <typename Result>
|
| 38 |
+
struct NoSequencer final : public Sequencer<Result> {
|
| 39 |
+
using typename Sequencer<Result>::ResultProducer;
|
| 40 |
+
optional<Result> next(ResultProducer next_result) override {
|
| 41 |
+
return next_result();
|
| 42 |
+
}
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
/// A `Sequencer` that buffers results and returns them in order of their
|
| 46 |
+
/// sequence number. The `OrderedSequencer` maintains an internal, monotonically
|
| 47 |
+
/// incrementing counter for the next sequence number it expects. If it receives
|
| 48 |
+
/// a result with a higher sequence number, it will buffer it for later (when
|
| 49 |
+
/// the sequence number reaches that of this result). Otherwise, if the sequence
|
| 50 |
+
/// numbers match, the result is returned.
|
| 51 |
+
///
|
| 52 |
+
/// Implementation note: The `OrderedSequencer` is implemented with a fixed-size
|
| 53 |
+
/// buffer. Let `m` be the maximum number of jobs in the data loader's queue and
|
| 54 |
+
/// `s` be the current sequence number. Assume `m` jobs are scheduled in the
|
| 55 |
+
/// `DataLoader`. Any new result is stored at index `job.sqn mod m` in the
|
| 56 |
+
/// `OrderedSequencer`. Why are we sure sequence numbers of new jobs will not
|
| 57 |
+
/// collide with sequence numbers of buffered jobs? The `OrderedSequencer` will
|
| 58 |
+
/// not return from `next()` until it receives the result with sqn `s`. This
|
| 59 |
+
/// means no new jobs can be scheduled in the `DataLoader` in the meantime,
|
| 60 |
+
/// which enforces that as long as sqn `s` has not been received, `s + m` (which
|
| 61 |
+
/// would cause a collision in the fixed-size buffer) will not yet be scheduled.
|
| 62 |
+
template <typename Result>
|
| 63 |
+
struct OrderedSequencer : public Sequencer<Result> {
|
| 64 |
+
using typename Sequencer<Result>::ResultProducer;
|
| 65 |
+
|
| 66 |
+
/// Constructs the `OrderedSequencer` with the maximum number of results it
|
| 67 |
+
/// will ever hold at one point in time.
|
| 68 |
+
explicit OrderedSequencer(size_t max_jobs) : buffer_(max_jobs) {}
|
| 69 |
+
|
| 70 |
+
/// Buffers results until the next one in the expected order is received.
|
| 71 |
+
optional<Result> next(ResultProducer next_result) override {
|
| 72 |
+
// If we already have the result for the next sqn, return it.
|
| 73 |
+
if (auto& maybe_result = buffer(next_sequence_number_)) {
|
| 74 |
+
auto result = std::move(*maybe_result);
|
| 75 |
+
buffer(next_sequence_number_++).reset();
|
| 76 |
+
return result;
|
| 77 |
+
}
|
| 78 |
+
// Otherwise wait for the next result.
|
| 79 |
+
while (true) {
|
| 80 |
+
auto result = next_result();
|
| 81 |
+
if (!result) {
|
| 82 |
+
AT_ASSERT(!detail::buffer_contains_result(buffer_));
|
| 83 |
+
break;
|
| 84 |
+
}
|
| 85 |
+
// If it was not nullopt and the sequence numbers match, return it
|
| 86 |
+
// directly and bump the sequence number.
|
| 87 |
+
if (result->sequence_number == next_sequence_number_) {
|
| 88 |
+
++next_sequence_number_;
|
| 89 |
+
return result;
|
| 90 |
+
}
|
| 91 |
+
// Stash the result for later.
|
| 92 |
+
AT_ASSERT(!buffer(result->sequence_number).has_value());
|
| 93 |
+
buffer(result->sequence_number) = std::move(result);
|
| 94 |
+
}
|
| 95 |
+
// The result was an empty optional, so we are done with this epoch.
|
| 96 |
+
return nullopt;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
/// Accesses the buffer at the `index` modulo the buffer size.
|
| 100 |
+
optional<Result>& buffer(size_t index) {
|
| 101 |
+
return buffer_.at(index % buffer_.size());
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
/// The monotonically increasing sequence number we expect.
|
| 105 |
+
size_t next_sequence_number_ = 0;
|
| 106 |
+
|
| 107 |
+
/// A fixed-size buffer (after construction).
|
| 108 |
+
std::vector<optional<Result>> buffer_;
|
| 109 |
+
};
|
| 110 |
+
} // namespace sequencers
|
| 111 |
+
} // namespace detail
|
| 112 |
+
} // namespace data
|
| 113 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/example.h
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/types.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace data {
|
| 7 |
+
|
| 8 |
+
/// An `Example` from a dataset.
|
| 9 |
+
///
|
| 10 |
+
/// A dataset consists of data and an associated target (label).
|
| 11 |
+
template <typename Data = at::Tensor, typename Target = at::Tensor>
|
| 12 |
+
struct Example {
|
| 13 |
+
using DataType = Data;
|
| 14 |
+
using TargetType = Target;
|
| 15 |
+
|
| 16 |
+
Example() = default;
|
| 17 |
+
Example(Data data, Target target)
|
| 18 |
+
: data(std::move(data)), target(std::move(target)) {}
|
| 19 |
+
|
| 20 |
+
Data data;
|
| 21 |
+
Target target;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
namespace example {
|
| 25 |
+
using NoTarget = void;
|
| 26 |
+
} // namespace example
|
| 27 |
+
|
| 28 |
+
/// A specialization for `Example` that does not have a target.
|
| 29 |
+
///
|
| 30 |
+
/// This class exists so that code can be written for a templated `Example`
|
| 31 |
+
/// type, and work both for labeled and unlabeled datasets.
|
| 32 |
+
template <typename Data>
|
| 33 |
+
struct Example<Data, example::NoTarget> {
|
| 34 |
+
using DataType = Data;
|
| 35 |
+
using TargetType = example::NoTarget;
|
| 36 |
+
|
| 37 |
+
Example() = default;
|
| 38 |
+
/* implicit */ Example(Data data) : data(std::move(data)) {}
|
| 39 |
+
|
| 40 |
+
// When a DataLoader returns an Example like this, that example should be
|
| 41 |
+
// implicitly convertible to the underlying data type.
|
| 42 |
+
|
| 43 |
+
operator Data&() {
|
| 44 |
+
return data;
|
| 45 |
+
}
|
| 46 |
+
operator const Data&() const {
|
| 47 |
+
return data;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
Data data;
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
using TensorExample = Example<at::Tensor, example::NoTarget>;
|
| 54 |
+
} // namespace data
|
| 55 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/iterator.h
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/csrc/utils/variadic.h>
|
| 4 |
+
#include <torch/types.h>
|
| 5 |
+
|
| 6 |
+
#include <c10/util/Exception.h>
|
| 7 |
+
|
| 8 |
+
#include <functional>
|
| 9 |
+
#include <iterator>
|
| 10 |
+
#include <memory>
|
| 11 |
+
#include <type_traits>
|
| 12 |
+
#include <utility>
|
| 13 |
+
|
| 14 |
+
namespace torch {
|
| 15 |
+
namespace data {
|
| 16 |
+
namespace detail {
|
| 17 |
+
// For increased safety and more separated logic, this implementation of
|
| 18 |
+
// `Iterator` consists of a `ValidIterator` and a `SentinelIterator`. A
|
| 19 |
+
// `ValidIterator` yields new batches until the `DataLoader` is exhausted. While
|
| 20 |
+
// the `DataLoader` is not exhausted, `ValidIterator`s compare equal if they are
|
| 21 |
+
// the same object. When the `ValidIterator` becomes exhausted, it compares
|
| 22 |
+
// equal to the `SentinelIterator`, but not before. Half the code here is to
|
| 23 |
+
// implement double dispatch for the comparison. Got damnit, C++.
|
| 24 |
+
|
| 25 |
+
template <typename Batch>
|
| 26 |
+
struct ValidIterator;
|
| 27 |
+
|
| 28 |
+
template <typename Batch>
|
| 29 |
+
struct SentinelIterator;
|
| 30 |
+
|
| 31 |
+
/// Base class for the `ValidIterator` and `SentinelIterator`
|
| 32 |
+
template <typename Batch>
|
| 33 |
+
struct IteratorImpl {
|
| 34 |
+
virtual ~IteratorImpl() = default;
|
| 35 |
+
virtual void next() = 0;
|
| 36 |
+
virtual Batch& get() = 0;
|
| 37 |
+
virtual bool operator==(const IteratorImpl& other) const = 0;
|
| 38 |
+
virtual bool operator==(const ValidIterator<Batch>& other) const = 0;
|
| 39 |
+
virtual bool operator==(const SentinelIterator<Batch>& other) const = 0;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
template <typename Batch>
|
| 43 |
+
struct ValidIterator : public IteratorImpl<Batch> {
|
| 44 |
+
using BatchProducer = std::function<optional<Batch>()>;
|
| 45 |
+
|
| 46 |
+
explicit ValidIterator(BatchProducer next_batch)
|
| 47 |
+
: next_batch_(std::move(next_batch)) {}
|
| 48 |
+
|
| 49 |
+
/// Fetches the next batch.
|
| 50 |
+
void next() override {
|
| 51 |
+
// If we didn't get the very first batch yet, get it now.
|
| 52 |
+
lazy_initialize();
|
| 53 |
+
TORCH_CHECK(
|
| 54 |
+
batch_.has_value(), "Attempted to increment iterator past the end");
|
| 55 |
+
// Increment to the next batch.
|
| 56 |
+
batch_ = next_batch_();
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
/// Returns the current batch. The precondition for this operation to not
|
| 60 |
+
/// throw an exception is that it has been compared to the `SentinelIterator`
|
| 61 |
+
/// and did not compare equal.
|
| 62 |
+
Batch& get() override {
|
| 63 |
+
// If we didn't get the very first batch yet, get it now.
|
| 64 |
+
lazy_initialize();
|
| 65 |
+
TORCH_CHECK(
|
| 66 |
+
batch_.has_value(),
|
| 67 |
+
"Attempted to dereference iterator that was past the end");
|
| 68 |
+
return batch_.value();
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/// Does double dispatch.
|
| 72 |
+
bool operator==(const IteratorImpl<Batch>& other) const override {
|
| 73 |
+
return other == *this;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
/// A `ValidIterator` is equal to the `SentinelIterator` iff. the
|
| 77 |
+
/// `ValidIterator` has reached the end of the dataloader.
|
| 78 |
+
bool operator==(const SentinelIterator<Batch>& /* unused */) const override {
|
| 79 |
+
lazy_initialize();
|
| 80 |
+
return !batch_;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/// Returns true if the memory address of `other` equals that of `this`.
|
| 84 |
+
bool operator==(const ValidIterator<Batch>& other) const override {
|
| 85 |
+
return &other == this;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/// Gets the very first batch if it has not yet been fetched.
|
| 89 |
+
void lazy_initialize() const {
|
| 90 |
+
if (!initialized_) {
|
| 91 |
+
batch_ = next_batch_();
|
| 92 |
+
initialized_ = true;
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
BatchProducer next_batch_;
|
| 97 |
+
mutable optional<Batch> batch_;
|
| 98 |
+
mutable bool initialized_ = false;
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
template <typename Batch>
|
| 102 |
+
struct SentinelIterator : public IteratorImpl<Batch> {
|
| 103 |
+
void next() override {
|
| 104 |
+
AT_ERROR(
|
| 105 |
+
"Incrementing the DataLoader's past-the-end iterator is not allowed");
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
Batch& get() override {
|
| 109 |
+
AT_ERROR(
|
| 110 |
+
"Dereferencing the DataLoader's past-the-end iterator is not allowed");
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/// Does double dispatch.
|
| 114 |
+
bool operator==(const IteratorImpl<Batch>& other) const override {
|
| 115 |
+
return other == *this;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
/// Calls the comparison operator between `ValidIterator` and
|
| 119 |
+
/// `SentinelIterator`.
|
| 120 |
+
bool operator==(const ValidIterator<Batch>& other) const override {
|
| 121 |
+
return other == *this;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
/// Sentinel iterators always compare equal.
|
| 125 |
+
bool operator==(const SentinelIterator<Batch>& other) const override {
|
| 126 |
+
return true;
|
| 127 |
+
}
|
| 128 |
+
};
|
| 129 |
+
} // namespace detail
|
| 130 |
+
|
| 131 |
+
template <typename Batch>
|
| 132 |
+
class Iterator {
|
| 133 |
+
public:
|
| 134 |
+
// Type aliases to make the class recognized as a proper iterator.
|
| 135 |
+
using difference_type = std::ptrdiff_t;
|
| 136 |
+
using value_type = Batch;
|
| 137 |
+
using pointer = Batch*;
|
| 138 |
+
using reference = Batch&;
|
| 139 |
+
using iterator_category = std::input_iterator_tag;
|
| 140 |
+
|
| 141 |
+
explicit Iterator(std::unique_ptr<detail::IteratorImpl<Batch>> impl)
|
| 142 |
+
: impl_(std::move(impl)) {}
|
| 143 |
+
|
| 144 |
+
/// Increments the iterator.
|
| 145 |
+
/// Only permitted for valid iterators (not past the end).
|
| 146 |
+
Iterator& operator++() {
|
| 147 |
+
impl_->next();
|
| 148 |
+
return *this;
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
/// Returns the current batch.
|
| 152 |
+
/// Only permitted for valid iterators (not past the end).
|
| 153 |
+
Batch& operator*() {
|
| 154 |
+
return impl_->get();
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
/// Returns a pointer to the current batch.
|
| 158 |
+
/// Only permitted for valid iterators (not past the end).
|
| 159 |
+
Batch* operator->() {
|
| 160 |
+
return &impl_->get();
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
/// Compares two iterators for equality.
|
| 164 |
+
bool operator==(const Iterator& other) const {
|
| 165 |
+
return *impl_ == *other.impl_;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
/// Compares two iterators for inequality.
|
| 169 |
+
bool operator!=(const Iterator& other) const {
|
| 170 |
+
return !(*this == other);
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
private:
|
| 174 |
+
/// Points either to a `ValidIterator` or to a `SentinelIterator`.
|
| 175 |
+
std::shared_ptr<detail::IteratorImpl<Batch>> impl_;
|
| 176 |
+
};
|
| 177 |
+
} // namespace data
|
| 178 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/samplers.h
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/samplers/base.h>
|
| 4 |
+
#include <torch/data/samplers/custom_batch_request.h>
|
| 5 |
+
#include <torch/data/samplers/distributed.h>
|
| 6 |
+
#include <torch/data/samplers/random.h>
|
| 7 |
+
#include <torch/data/samplers/sequential.h>
|
| 8 |
+
#include <torch/data/samplers/serialize.h>
|
| 9 |
+
#include <torch/data/samplers/stream.h>
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/samplers/custom_batch_request.h
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/csrc/Export.h>
|
| 4 |
+
#include <cstddef>
|
| 5 |
+
|
| 6 |
+
namespace torch {
|
| 7 |
+
namespace data {
|
| 8 |
+
namespace samplers {
|
| 9 |
+
/// A base class for custom index types.
|
| 10 |
+
struct TORCH_API CustomBatchRequest {
|
| 11 |
+
CustomBatchRequest() = default;
|
| 12 |
+
CustomBatchRequest(const CustomBatchRequest&) = default;
|
| 13 |
+
CustomBatchRequest(CustomBatchRequest&&) noexcept = default;
|
| 14 |
+
virtual ~CustomBatchRequest() = default;
|
| 15 |
+
|
| 16 |
+
/// The number of elements accessed by this index.
|
| 17 |
+
virtual size_t size() const = 0;
|
| 18 |
+
};
|
| 19 |
+
} // namespace samplers
|
| 20 |
+
} // namespace data
|
| 21 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/samplers/random.h
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/csrc/Export.h>
|
| 4 |
+
#include <torch/data/samplers/base.h>
|
| 5 |
+
#include <torch/types.h>
|
| 6 |
+
|
| 7 |
+
#include <cstddef>
|
| 8 |
+
#include <vector>
|
| 9 |
+
|
| 10 |
+
namespace torch {
|
| 11 |
+
namespace serialize {
|
| 12 |
+
class OutputArchive;
|
| 13 |
+
class InputArchive;
|
| 14 |
+
} // namespace serialize
|
| 15 |
+
} // namespace torch
|
| 16 |
+
|
| 17 |
+
namespace torch {
|
| 18 |
+
namespace data {
|
| 19 |
+
namespace samplers {
|
| 20 |
+
|
| 21 |
+
/// A `Sampler` that returns random indices.
|
| 22 |
+
class TORCH_API RandomSampler : public Sampler<> {
|
| 23 |
+
public:
|
| 24 |
+
/// Constructs a `RandomSampler` with a size and dtype for the stored indices.
|
| 25 |
+
///
|
| 26 |
+
/// The constructor will eagerly allocate all required indices, which is the
|
| 27 |
+
/// sequence `0 ... size - 1`. `index_dtype` is the data type of the stored
|
| 28 |
+
/// indices. You can change it to influence memory usage.
|
| 29 |
+
explicit RandomSampler(int64_t size, Dtype index_dtype = torch::kInt64);
|
| 30 |
+
|
| 31 |
+
~RandomSampler() override;
|
| 32 |
+
|
| 33 |
+
/// Resets the `RandomSampler` to a new set of indices.
|
| 34 |
+
void reset(optional<size_t> new_size = nullopt) override;
|
| 35 |
+
|
| 36 |
+
/// Returns the next batch of indices.
|
| 37 |
+
optional<std::vector<size_t>> next(size_t batch_size) override;
|
| 38 |
+
|
| 39 |
+
/// Serializes the `RandomSampler` to the `archive`.
|
| 40 |
+
void save(serialize::OutputArchive& archive) const override;
|
| 41 |
+
|
| 42 |
+
/// Deserializes the `RandomSampler` from the `archive`.
|
| 43 |
+
void load(serialize::InputArchive& archive) override;
|
| 44 |
+
|
| 45 |
+
/// Returns the current index of the `RandomSampler`.
|
| 46 |
+
size_t index() const noexcept;
|
| 47 |
+
|
| 48 |
+
private:
|
| 49 |
+
at::Tensor indices_;
|
| 50 |
+
int64_t index_ = 0;
|
| 51 |
+
};
|
| 52 |
+
} // namespace samplers
|
| 53 |
+
} // namespace data
|
| 54 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms.h
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/transforms/base.h>
|
| 4 |
+
#include <torch/data/transforms/collate.h>
|
| 5 |
+
#include <torch/data/transforms/lambda.h>
|
| 6 |
+
#include <torch/data/transforms/stack.h>
|
| 7 |
+
#include <torch/data/transforms/tensor.h>
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms/collate.h
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/example.h>
|
| 4 |
+
#include <torch/data/transforms/lambda.h>
|
| 5 |
+
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
namespace torch {
|
| 9 |
+
namespace data {
|
| 10 |
+
namespace transforms {
|
| 11 |
+
|
| 12 |
+
/// A `Collation` is a transform that reduces a batch into a single value.
|
| 13 |
+
/// The result is a `BatchDataset` that has the type of the single value as its
|
| 14 |
+
/// `BatchType`.
|
| 15 |
+
template <typename T, typename BatchType = std::vector<T>>
|
| 16 |
+
using Collation = BatchTransform<BatchType, T>;
|
| 17 |
+
|
| 18 |
+
/// A `Collate` allows passing a custom function to reduce/collate a batch
|
| 19 |
+
/// into a single value. It's effectively the lambda version of `Collation`,
|
| 20 |
+
/// which you could subclass and override `operator()` to achieve the same.
|
| 21 |
+
///
|
| 22 |
+
/// \rst
|
| 23 |
+
/// .. code-block:: cpp
|
| 24 |
+
/// using namespace torch::data;
|
| 25 |
+
///
|
| 26 |
+
/// auto dataset = datasets::MNIST("path/to/mnist")
|
| 27 |
+
/// .map(transforms::Collate<Example<>>([](std::vector<Example<>> e) {
|
| 28 |
+
/// return std::move(e.front());
|
| 29 |
+
/// }));
|
| 30 |
+
/// \endrst
|
| 31 |
+
template <typename T, typename BatchType = std::vector<T>>
|
| 32 |
+
using Collate = BatchLambda<BatchType, T>;
|
| 33 |
+
} // namespace transforms
|
| 34 |
+
} // namespace data
|
| 35 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms/lambda.h
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/transforms/base.h>
|
| 4 |
+
|
| 5 |
+
#include <functional>
|
| 6 |
+
#include <utility>
|
| 7 |
+
#include <vector>
|
| 8 |
+
|
| 9 |
+
namespace torch {
|
| 10 |
+
namespace data {
|
| 11 |
+
namespace transforms {
|
| 12 |
+
|
| 13 |
+
/// A `BatchTransform` that applies a user-provided functor to a batch.
|
| 14 |
+
template <typename Input, typename Output = Input>
|
| 15 |
+
class BatchLambda : public BatchTransform<Input, Output> {
|
| 16 |
+
public:
|
| 17 |
+
using typename BatchTransform<Input, Output>::InputBatchType;
|
| 18 |
+
using typename BatchTransform<Input, Output>::OutputBatchType;
|
| 19 |
+
using FunctionType = std::function<OutputBatchType(InputBatchType)>;
|
| 20 |
+
|
| 21 |
+
/// Constructs the `BatchLambda` from the given `function` object.
|
| 22 |
+
explicit BatchLambda(FunctionType function)
|
| 23 |
+
: function_(std::move(function)) {}
|
| 24 |
+
|
| 25 |
+
/// Applies the user-provided function object to the `input_batch`.
|
| 26 |
+
OutputBatchType apply_batch(InputBatchType input_batch) override {
|
| 27 |
+
return function_(std::move(input_batch));
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
private:
|
| 31 |
+
FunctionType function_;
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
// A `Transform` that applies a user-provided functor to individual examples.
|
| 35 |
+
template <typename Input, typename Output = Input>
|
| 36 |
+
class Lambda : public Transform<Input, Output> {
|
| 37 |
+
public:
|
| 38 |
+
using typename Transform<Input, Output>::InputType;
|
| 39 |
+
using typename Transform<Input, Output>::OutputType;
|
| 40 |
+
using FunctionType = std::function<Output(Input)>;
|
| 41 |
+
|
| 42 |
+
/// Constructs the `Lambda` from the given `function` object.
|
| 43 |
+
explicit Lambda(FunctionType function) : function_(std::move(function)) {}
|
| 44 |
+
|
| 45 |
+
/// Applies the user-provided function object to the `input`.
|
| 46 |
+
OutputType apply(InputType input) override {
|
| 47 |
+
return function_(std::move(input));
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
private:
|
| 51 |
+
FunctionType function_;
|
| 52 |
+
};
|
| 53 |
+
|
| 54 |
+
} // namespace transforms
|
| 55 |
+
} // namespace data
|
| 56 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/transforms/stack.h
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/data/example.h>
|
| 4 |
+
#include <torch/data/transforms/collate.h>
|
| 5 |
+
#include <torch/types.h>
|
| 6 |
+
|
| 7 |
+
#include <utility>
|
| 8 |
+
#include <vector>
|
| 9 |
+
|
| 10 |
+
namespace torch {
|
| 11 |
+
namespace data {
|
| 12 |
+
namespace transforms {
|
| 13 |
+
|
| 14 |
+
template <typename T = Example<>>
|
| 15 |
+
struct Stack;
|
| 16 |
+
|
| 17 |
+
/// A `Collation` for `Example<Tensor, Tensor>` types that stacks all data
|
| 18 |
+
/// tensors into one tensor, and all target (label) tensors into one tensor.
|
| 19 |
+
template <>
|
| 20 |
+
struct Stack<Example<>> : public Collation<Example<>> {
|
| 21 |
+
Example<> apply_batch(std::vector<Example<>> examples) override {
|
| 22 |
+
std::vector<torch::Tensor> data, targets;
|
| 23 |
+
data.reserve(examples.size());
|
| 24 |
+
targets.reserve(examples.size());
|
| 25 |
+
for (auto& example : examples) {
|
| 26 |
+
data.push_back(std::move(example.data));
|
| 27 |
+
targets.push_back(std::move(example.target));
|
| 28 |
+
}
|
| 29 |
+
return {torch::stack(data), torch::stack(targets)};
|
| 30 |
+
}
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
/// A `Collation` for `Example<Tensor, NoTarget>` types that stacks all data
|
| 34 |
+
/// tensors into one tensor.
|
| 35 |
+
template <>
|
| 36 |
+
struct Stack<TensorExample>
|
| 37 |
+
: public Collation<Example<Tensor, example::NoTarget>> {
|
| 38 |
+
TensorExample apply_batch(std::vector<TensorExample> examples) override {
|
| 39 |
+
std::vector<torch::Tensor> data;
|
| 40 |
+
data.reserve(examples.size());
|
| 41 |
+
for (auto& example : examples) {
|
| 42 |
+
data.push_back(std::move(example.data));
|
| 43 |
+
}
|
| 44 |
+
return torch::stack(data);
|
| 45 |
+
}
|
| 46 |
+
};
|
| 47 |
+
} // namespace transforms
|
| 48 |
+
} // namespace data
|
| 49 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/data/worker_exception.h
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <exception>
|
| 4 |
+
#include <string>
|
| 5 |
+
#include <utility>
|
| 6 |
+
|
| 7 |
+
namespace torch {
|
| 8 |
+
namespace data {
|
| 9 |
+
|
| 10 |
+
/// An exception thrown when a DataLoader's worker thread throws an exception,
|
| 11 |
+
/// which is caught. A `WorkerException` stores an `exception_ptr` to the
|
| 12 |
+
/// original exception thrown in the worker thread.
|
| 13 |
+
struct WorkerException : public std::exception {
|
| 14 |
+
/// Constructs a `WorkerException` from an `exception_ptr`.
|
| 15 |
+
explicit WorkerException(std::exception_ptr original)
|
| 16 |
+
: original_exception(std::move(original)),
|
| 17 |
+
message("Caught exception in DataLoader worker thread.") {
|
| 18 |
+
try {
|
| 19 |
+
std::rethrow_exception(original_exception);
|
| 20 |
+
} catch (std::exception& e) {
|
| 21 |
+
message += " Original message: ";
|
| 22 |
+
message += e.what();
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
const char* what() const noexcept override {
|
| 27 |
+
return message.c_str();
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
/// The original exception thrown in the worker thread.
|
| 31 |
+
std::exception_ptr original_exception;
|
| 32 |
+
|
| 33 |
+
/// This exception's message (not the original exception's message).
|
| 34 |
+
std::string message;
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
} // namespace data
|
| 38 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/activation.h
ADDED
|
@@ -0,0 +1,966 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/Dispatch.h>
|
| 4 |
+
#include <torch/nn/functional/dropout.h>
|
| 5 |
+
#include <torch/nn/functional/linear.h>
|
| 6 |
+
#include <torch/nn/options/activation.h>
|
| 7 |
+
#include <torch/nn/options/dropout.h>
|
| 8 |
+
#include <torch/nn/options/linear.h>
|
| 9 |
+
#include <torch/types.h>
|
| 10 |
+
#include <limits>
|
| 11 |
+
#include <utility>
|
| 12 |
+
|
| 13 |
+
namespace torch {
|
| 14 |
+
namespace nn {
|
| 15 |
+
namespace functional {
|
| 16 |
+
|
| 17 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 18 |
+
namespace detail {
|
| 19 |
+
inline Tensor elu(Tensor input, double alpha, bool inplace) {
|
| 20 |
+
if (inplace) {
|
| 21 |
+
return torch::elu_(input, alpha);
|
| 22 |
+
} else {
|
| 23 |
+
return torch::elu(input, alpha);
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
} // namespace detail
|
| 27 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 28 |
+
|
| 29 |
+
/// See
|
| 30 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.elu
|
| 31 |
+
/// about the exact behavior of this functional.
|
| 32 |
+
///
|
| 33 |
+
/// See the documentation for `torch::nn::functional::ELUFuncOptions` class to
|
| 34 |
+
/// learn what optional arguments are supported for this functional.
|
| 35 |
+
///
|
| 36 |
+
/// Example:
|
| 37 |
+
/// ```
|
| 38 |
+
/// namespace F = torch::nn::functional;
|
| 39 |
+
/// F::elu(x, F::ELUFuncOptions().alpha(0.42).inplace(true));
|
| 40 |
+
/// ```
|
| 41 |
+
inline Tensor elu(Tensor input, const ELUFuncOptions& options = {}) {
|
| 42 |
+
return detail::elu(std::move(input), options.alpha(), options.inplace());
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// ============================================================================
|
| 46 |
+
|
| 47 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 48 |
+
namespace detail {
|
| 49 |
+
inline Tensor selu(Tensor input, bool inplace) {
|
| 50 |
+
if (inplace) {
|
| 51 |
+
return torch::selu_(input);
|
| 52 |
+
} else {
|
| 53 |
+
return torch::selu(input);
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
} // namespace detail
|
| 57 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 58 |
+
|
| 59 |
+
/// See
|
| 60 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.selu
|
| 61 |
+
/// about the exact behavior of this functional.
|
| 62 |
+
///
|
| 63 |
+
/// See the documentation for `torch::nn::functional::SELUFuncOptions` class to
|
| 64 |
+
/// learn what optional arguments are supported for this functional.
|
| 65 |
+
///
|
| 66 |
+
/// Example:
|
| 67 |
+
/// ```
|
| 68 |
+
/// namespace F = torch::nn::functional;
|
| 69 |
+
/// F::selu(input, F::SELUFuncOptions(false));
|
| 70 |
+
/// ```
|
| 71 |
+
inline Tensor selu(Tensor input, const SELUFuncOptions& options = {}) {
|
| 72 |
+
return detail::selu(std::move(input), options.inplace());
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// ============================================================================
|
| 76 |
+
|
| 77 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 78 |
+
namespace detail {
|
| 79 |
+
inline Tensor hardshrink(const Tensor& input, double lambda) {
|
| 80 |
+
return torch::hardshrink(input, lambda);
|
| 81 |
+
}
|
| 82 |
+
} // namespace detail
|
| 83 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 84 |
+
|
| 85 |
+
/// See
|
| 86 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.hardshrink
|
| 87 |
+
/// about the exact behavior of this functional.
|
| 88 |
+
///
|
| 89 |
+
/// See the documentation for `torch::nn::functional::HardshrinkFuncOptions`
|
| 90 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 91 |
+
///
|
| 92 |
+
/// Example:
|
| 93 |
+
/// ```
|
| 94 |
+
/// namespace F = torch::nn::functional;
|
| 95 |
+
/// F::hardshrink(x, F::HardshrinkFuncOptions().lambda(0.42));
|
| 96 |
+
/// ```
|
| 97 |
+
inline Tensor hardshrink(
|
| 98 |
+
const Tensor& input,
|
| 99 |
+
const HardshrinkFuncOptions& options = {}) {
|
| 100 |
+
return detail::hardshrink(input, options.lambda());
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// ============================================================================
|
| 104 |
+
|
| 105 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 106 |
+
namespace detail {
|
| 107 |
+
inline Tensor hardtanh(
|
| 108 |
+
Tensor input,
|
| 109 |
+
double min_val,
|
| 110 |
+
double max_val,
|
| 111 |
+
bool inplace) {
|
| 112 |
+
if (inplace) {
|
| 113 |
+
return torch::hardtanh_(input, min_val, max_val);
|
| 114 |
+
} else {
|
| 115 |
+
return torch::hardtanh(input, min_val, max_val);
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
} // namespace detail
|
| 119 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 120 |
+
|
| 121 |
+
/// See
|
| 122 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.hardtanh
|
| 123 |
+
/// about the exact behavior of this functional.
|
| 124 |
+
///
|
| 125 |
+
/// See the documentation for `torch::nn::functional::HardtanhFuncOptions` class
|
| 126 |
+
/// to learn what optional arguments are supported for this functional.
|
| 127 |
+
///
|
| 128 |
+
/// Example:
|
| 129 |
+
/// ```
|
| 130 |
+
/// namespace F = torch::nn::functional;
|
| 131 |
+
/// F::hardtanh(x,
|
| 132 |
+
/// F::HardtanhFuncOptions().min_val(-1.0).max_val(1.0).inplace(true));
|
| 133 |
+
/// ```
|
| 134 |
+
inline Tensor hardtanh(Tensor input, const HardtanhFuncOptions& options = {}) {
|
| 135 |
+
return detail::hardtanh(
|
| 136 |
+
std::move(input),
|
| 137 |
+
options.min_val(),
|
| 138 |
+
options.max_val(),
|
| 139 |
+
options.inplace());
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// ============================================================================
|
| 143 |
+
|
| 144 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 145 |
+
namespace detail {
|
| 146 |
+
inline Tensor leaky_relu(Tensor input, double negative_slope, bool inplace) {
|
| 147 |
+
if (inplace) {
|
| 148 |
+
return torch::leaky_relu_(input, negative_slope);
|
| 149 |
+
} else {
|
| 150 |
+
return torch::leaky_relu(input, negative_slope);
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
} // namespace detail
|
| 154 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 155 |
+
|
| 156 |
+
/// See
|
| 157 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.leaky_relu
|
| 158 |
+
/// about the exact behavior of this functional.
|
| 159 |
+
///
|
| 160 |
+
/// See the documentation for `torch::nn::functional::LeakyReLUFuncOptions`
|
| 161 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 162 |
+
///
|
| 163 |
+
/// Example:
|
| 164 |
+
/// ```
|
| 165 |
+
/// namespace F = torch::nn::functional;
|
| 166 |
+
/// F::leaky_relu(x,
|
| 167 |
+
/// F::LeakyReLUFuncOptions().negative_slope(0.42).inplace(true));
|
| 168 |
+
/// ```
|
| 169 |
+
inline Tensor leaky_relu(
|
| 170 |
+
Tensor input,
|
| 171 |
+
const LeakyReLUFuncOptions& options = {}) {
|
| 172 |
+
return detail::leaky_relu(
|
| 173 |
+
std::move(input), options.negative_slope(), options.inplace());
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// ============================================================================
|
| 177 |
+
|
| 178 |
+
inline Tensor logsigmoid(const Tensor& input) {
|
| 179 |
+
return torch::log_sigmoid(input);
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
// ============================================================================
|
| 183 |
+
|
| 184 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 185 |
+
namespace detail {
|
| 186 |
+
inline Tensor gumbel_softmax(
|
| 187 |
+
const Tensor& logits,
|
| 188 |
+
double tau,
|
| 189 |
+
bool hard,
|
| 190 |
+
int dim) {
|
| 191 |
+
auto gumbels =
|
| 192 |
+
-torch::empty_like(logits).exponential_().log(); // ~Gumbel(0,1)
|
| 193 |
+
gumbels = (logits + gumbels) / tau; // ~Gumbel(logits, tau)
|
| 194 |
+
auto y_soft = gumbels.softmax(dim);
|
| 195 |
+
|
| 196 |
+
torch::Tensor ret;
|
| 197 |
+
if (hard) {
|
| 198 |
+
// Straight through.
|
| 199 |
+
auto index = std::get<1>(y_soft.max(dim, /*keepdim=*/true));
|
| 200 |
+
auto y_hard = torch::zeros_like(logits).scatter_(dim, index, 1.0);
|
| 201 |
+
ret = y_hard - y_soft.detach() + y_soft;
|
| 202 |
+
} else {
|
| 203 |
+
ret = y_soft;
|
| 204 |
+
}
|
| 205 |
+
return ret;
|
| 206 |
+
}
|
| 207 |
+
} // namespace detail
|
| 208 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 209 |
+
|
| 210 |
+
/// See
|
| 211 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.gumbel_softmax
|
| 212 |
+
/// about the exact behavior of this functional.
|
| 213 |
+
///
|
| 214 |
+
/// See the documentation for `torch::nn::functional::GumbelSoftmaxFuncOptions`
|
| 215 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 216 |
+
///
|
| 217 |
+
/// Example:
|
| 218 |
+
/// ```
|
| 219 |
+
/// namespace F = torch::nn::functional;
|
| 220 |
+
/// F::gumbel_softmax(logits, F::GumbelSoftmaxFuncOptions().hard(true).dim(-1));
|
| 221 |
+
/// ```
|
| 222 |
+
inline Tensor gumbel_softmax(
|
| 223 |
+
const Tensor& logits,
|
| 224 |
+
const GumbelSoftmaxFuncOptions& options = {}) {
|
| 225 |
+
return detail::gumbel_softmax(
|
| 226 |
+
logits, options.tau(), options.hard(), options.dim());
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// ============================================================================
|
| 230 |
+
|
| 231 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 232 |
+
namespace detail {
|
| 233 |
+
inline Tensor softmax(
|
| 234 |
+
const Tensor& input,
|
| 235 |
+
int64_t dim,
|
| 236 |
+
c10::optional<torch::Dtype> dtype) {
|
| 237 |
+
Tensor ret;
|
| 238 |
+
|
| 239 |
+
if (dtype == c10::nullopt) {
|
| 240 |
+
ret = input.softmax(dim);
|
| 241 |
+
} else {
|
| 242 |
+
ret = input.softmax(dim, dtype);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
return ret;
|
| 246 |
+
}
|
| 247 |
+
} // namespace detail
|
| 248 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 249 |
+
|
| 250 |
+
/// See
|
| 251 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.softmax
|
| 252 |
+
/// about the exact behavior of this functional.
|
| 253 |
+
///
|
| 254 |
+
/// See the documentation for `torch::nn::functional::SoftmaxFuncOptions` class
|
| 255 |
+
/// to learn what optional arguments are supported for this functional.
|
| 256 |
+
///
|
| 257 |
+
/// Example:
|
| 258 |
+
/// ```
|
| 259 |
+
/// namespace F = torch::nn::functional;
|
| 260 |
+
/// F::softmax(input, F::SoftmaxFuncOptions(1));
|
| 261 |
+
/// ```
|
| 262 |
+
inline Tensor softmax(const Tensor& input, const SoftmaxFuncOptions& options) {
|
| 263 |
+
return detail::softmax(input, options.dim(), options.dtype());
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
// ============================================================================
|
| 267 |
+
|
| 268 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 269 |
+
namespace detail {
|
| 270 |
+
inline Tensor softmin(
|
| 271 |
+
const Tensor& input,
|
| 272 |
+
int64_t dim,
|
| 273 |
+
c10::optional<torch::Dtype> dtype) {
|
| 274 |
+
Tensor ret;
|
| 275 |
+
|
| 276 |
+
if (dtype == c10::nullopt) {
|
| 277 |
+
ret = (-input).softmax(dim);
|
| 278 |
+
} else {
|
| 279 |
+
ret = (-input).softmax(dim, dtype);
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
return ret;
|
| 283 |
+
}
|
| 284 |
+
} // namespace detail
|
| 285 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 286 |
+
|
| 287 |
+
/// See
|
| 288 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.softmin
|
| 289 |
+
/// about the exact behavior of this functional.
|
| 290 |
+
///
|
| 291 |
+
/// See the documentation for `torch::nn::functional::SoftminFuncOptions` class
|
| 292 |
+
/// to learn what optional arguments are supported for this functional.
|
| 293 |
+
///
|
| 294 |
+
/// Example:
|
| 295 |
+
/// ```
|
| 296 |
+
/// namespace F = torch::nn::functional;
|
| 297 |
+
/// F::softmin(input, F::SoftminFuncOptions(1));
|
| 298 |
+
/// ```
|
| 299 |
+
inline Tensor softmin(const Tensor& input, const SoftminFuncOptions& options) {
|
| 300 |
+
return detail::softmin(input, options.dim(), options.dtype());
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
// ============================================================================
|
| 304 |
+
|
| 305 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 306 |
+
namespace detail {
|
| 307 |
+
inline Tensor log_softmax(
|
| 308 |
+
const Tensor& input,
|
| 309 |
+
int64_t dim,
|
| 310 |
+
c10::optional<torch::Dtype> dtype) {
|
| 311 |
+
Tensor ret;
|
| 312 |
+
|
| 313 |
+
if (dtype == c10::nullopt) {
|
| 314 |
+
ret = input.log_softmax(dim);
|
| 315 |
+
} else {
|
| 316 |
+
ret = input.log_softmax(dim, dtype);
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
return ret;
|
| 320 |
+
}
|
| 321 |
+
} // namespace detail
|
| 322 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 323 |
+
|
| 324 |
+
/// See
|
| 325 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.log_softmax
|
| 326 |
+
/// about the exact behavior of this functional.
|
| 327 |
+
///
|
| 328 |
+
/// See the documentation for `torch::nn::functional::LogSoftmaxFuncOptions`
|
| 329 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 330 |
+
///
|
| 331 |
+
/// Example:
|
| 332 |
+
/// ```
|
| 333 |
+
/// namespace F = torch::nn::functional;
|
| 334 |
+
/// F::log_softmax(input, LogSoftmaxFuncOptions(1));
|
| 335 |
+
/// ```
|
| 336 |
+
inline Tensor log_softmax(
|
| 337 |
+
const Tensor& input,
|
| 338 |
+
const LogSoftmaxFuncOptions& options) {
|
| 339 |
+
return detail::log_softmax(input, options.dim(), options.dtype());
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
// ============================================================================
|
| 343 |
+
|
| 344 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 345 |
+
namespace detail {
|
| 346 |
+
inline Tensor glu(const Tensor& input, int64_t dim) {
|
| 347 |
+
TORCH_CHECK(
|
| 348 |
+
input.dim() != 0,
|
| 349 |
+
"glu does not suppport scalars because halving size must be even");
|
| 350 |
+
return torch::glu(input, dim);
|
| 351 |
+
}
|
| 352 |
+
} // namespace detail
|
| 353 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 354 |
+
|
| 355 |
+
/// See
|
| 356 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.glu
|
| 357 |
+
/// about the exact behavior of this functional.
|
| 358 |
+
///
|
| 359 |
+
/// See the documentation for `torch::nn::functional::GLUFuncOptions` class to
|
| 360 |
+
/// learn what optional arguments are supported for this functional.
|
| 361 |
+
///
|
| 362 |
+
/// Example:
|
| 363 |
+
/// ```
|
| 364 |
+
/// namespace F = torch::nn::functional;
|
| 365 |
+
/// F::glu(input, GLUFuncOptions(1));
|
| 366 |
+
/// ```
|
| 367 |
+
inline Tensor glu(const Tensor& input, const GLUFuncOptions& options = {}) {
|
| 368 |
+
return detail::glu(input, options.dim());
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
// ============================================================================
|
| 372 |
+
|
| 373 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 374 |
+
namespace detail {
|
| 375 |
+
inline Tensor gelu(const Tensor& input, string approximate) {
|
| 376 |
+
return torch::gelu(input, approximate);
|
| 377 |
+
}
|
| 378 |
+
} // namespace detail
|
| 379 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 380 |
+
|
| 381 |
+
inline Tensor gelu(const Tensor& input, const GELUFuncOptions& options = {}) {
|
| 382 |
+
return detail::gelu(input, options.approximate());
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
// ============================================================================
|
| 386 |
+
|
| 387 |
+
inline Tensor silu(const Tensor& input) {
|
| 388 |
+
return torch::silu(input);
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
// ============================================================================
|
| 392 |
+
|
| 393 |
+
inline Tensor mish(const Tensor& input) {
|
| 394 |
+
return torch::mish(input);
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
// ============================================================================
|
| 398 |
+
|
| 399 |
+
inline Tensor prelu(const Tensor& input, const Tensor& weight) {
|
| 400 |
+
return torch::prelu(input, weight);
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
// ============================================================================
|
| 404 |
+
|
| 405 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 406 |
+
namespace detail {
|
| 407 |
+
inline Tensor relu(Tensor input, bool inplace) {
|
| 408 |
+
if (inplace) {
|
| 409 |
+
return torch::relu_(input);
|
| 410 |
+
} else {
|
| 411 |
+
return torch::relu(input);
|
| 412 |
+
}
|
| 413 |
+
}
|
| 414 |
+
} // namespace detail
|
| 415 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 416 |
+
|
| 417 |
+
/// See
|
| 418 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.relu
|
| 419 |
+
/// about the exact behavior of this functional.
|
| 420 |
+
///
|
| 421 |
+
/// See the documentation for `torch::nn::functional::ReLUFuncOptions` class to
|
| 422 |
+
/// learn what optional arguments are supported for this functional.
|
| 423 |
+
///
|
| 424 |
+
/// Example:
|
| 425 |
+
/// ```
|
| 426 |
+
/// namespace F = torch::nn::functional;
|
| 427 |
+
/// F::relu(x, F::ReLUFuncOptions().inplace(true));
|
| 428 |
+
/// ```
|
| 429 |
+
inline Tensor relu(Tensor input, const ReLUFuncOptions& options = {}) {
|
| 430 |
+
return detail::relu(std::move(input), options.inplace());
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
// ============================================================================
|
| 434 |
+
|
| 435 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 436 |
+
namespace detail {
|
| 437 |
+
inline Tensor relu6(Tensor input, bool inplace) {
|
| 438 |
+
if (inplace) {
|
| 439 |
+
return torch::relu6_(input);
|
| 440 |
+
} else {
|
| 441 |
+
return torch::relu6(input);
|
| 442 |
+
}
|
| 443 |
+
}
|
| 444 |
+
} // namespace detail
|
| 445 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 446 |
+
|
| 447 |
+
/// See
|
| 448 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.relu6
|
| 449 |
+
/// about the exact behavior of this functional.
|
| 450 |
+
///
|
| 451 |
+
/// See the documentation for `torch::nn::functional::ReLU6FuncOptions` class to
|
| 452 |
+
/// learn what optional arguments are supported for this functional.
|
| 453 |
+
///
|
| 454 |
+
/// Example:
|
| 455 |
+
/// ```
|
| 456 |
+
/// namespace F = torch::nn::functional;
|
| 457 |
+
/// F::relu6(x, F::ReLU6FuncOptions().inplace(true));
|
| 458 |
+
/// ```
|
| 459 |
+
inline Tensor relu6(Tensor input, const ReLU6FuncOptions& options = {}) {
|
| 460 |
+
return detail::relu6(std::move(input), options.inplace());
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
// ============================================================================
|
| 464 |
+
|
| 465 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 466 |
+
namespace detail {
|
| 467 |
+
inline Tensor rrelu(
|
| 468 |
+
Tensor input,
|
| 469 |
+
double lower,
|
| 470 |
+
double upper,
|
| 471 |
+
bool training,
|
| 472 |
+
bool inplace) {
|
| 473 |
+
if (inplace) {
|
| 474 |
+
return torch::rrelu_(input, lower, upper, training);
|
| 475 |
+
} else {
|
| 476 |
+
return torch::rrelu(input, lower, upper, training);
|
| 477 |
+
}
|
| 478 |
+
}
|
| 479 |
+
} // namespace detail
|
| 480 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 481 |
+
|
| 482 |
+
/// See
|
| 483 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.rrelu
|
| 484 |
+
/// about the exact behavior of this functional.
|
| 485 |
+
///
|
| 486 |
+
/// See the documentation for `torch::nn::functional::RReLUFuncOptions` class to
|
| 487 |
+
/// learn what optional arguments are supported for this functional.
|
| 488 |
+
///
|
| 489 |
+
/// Example:
|
| 490 |
+
/// ```
|
| 491 |
+
/// namespace F = torch::nn::functional;
|
| 492 |
+
/// F::rrelu(x, F::RReLUFuncOptions().lower(0.1).upper(0.4).inplace(true));
|
| 493 |
+
/// ```
|
| 494 |
+
inline Tensor rrelu(Tensor input, const RReLUFuncOptions& options = {}) {
|
| 495 |
+
return detail::rrelu(
|
| 496 |
+
std::move(input),
|
| 497 |
+
options.lower(),
|
| 498 |
+
options.upper(),
|
| 499 |
+
options.training(),
|
| 500 |
+
options.inplace());
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
// ============================================================================
|
| 504 |
+
|
| 505 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 506 |
+
namespace detail {
|
| 507 |
+
inline Tensor celu(Tensor input, double alpha, bool inplace) {
|
| 508 |
+
if (inplace) {
|
| 509 |
+
return torch::celu_(input, alpha);
|
| 510 |
+
} else {
|
| 511 |
+
return torch::celu(input, alpha);
|
| 512 |
+
}
|
| 513 |
+
}
|
| 514 |
+
} // namespace detail
|
| 515 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 516 |
+
|
| 517 |
+
/// See
|
| 518 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.celu
|
| 519 |
+
/// about the exact behavior of this functional.
|
| 520 |
+
///
|
| 521 |
+
/// See the documentation for `torch::nn::functional::CELUFuncOptions` class to
|
| 522 |
+
/// learn what optional arguments are supported for this functional.
|
| 523 |
+
///
|
| 524 |
+
/// Example:
|
| 525 |
+
/// ```
|
| 526 |
+
/// namespace F = torch::nn::functional;
|
| 527 |
+
/// F::celu(x, F::CELUFuncOptions().alpha(0.42).inplace(true));
|
| 528 |
+
/// ```
|
| 529 |
+
inline Tensor celu(Tensor input, const CELUFuncOptions& options = {}) {
|
| 530 |
+
return detail::celu(std::move(input), options.alpha(), options.inplace());
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
// ============================================================================
|
| 534 |
+
|
| 535 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 536 |
+
namespace detail {
|
| 537 |
+
inline Tensor softplus(const Tensor& input, double beta, double threshold) {
|
| 538 |
+
return torch::softplus(input, beta, threshold);
|
| 539 |
+
}
|
| 540 |
+
} // namespace detail
|
| 541 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 542 |
+
|
| 543 |
+
/// See
|
| 544 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.softplus
|
| 545 |
+
/// about the exact behavior of this functional.
|
| 546 |
+
///
|
| 547 |
+
/// See the documentation for `torch::nn::functional::SoftplusFuncOptions` class
|
| 548 |
+
/// to learn what optional arguments are supported for this functional.
|
| 549 |
+
///
|
| 550 |
+
/// Example:
|
| 551 |
+
/// ```
|
| 552 |
+
/// namespace F = torch::nn::functional;
|
| 553 |
+
/// F::softplus(x, F::SoftplusFuncOptions().beta(0.5).threshold(3.0));
|
| 554 |
+
/// ```
|
| 555 |
+
inline Tensor softplus(
|
| 556 |
+
const Tensor& input,
|
| 557 |
+
const SoftplusFuncOptions& options = {}) {
|
| 558 |
+
return detail::softplus(input, options.beta(), options.threshold());
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
// ============================================================================
|
| 562 |
+
|
| 563 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 564 |
+
namespace detail {
|
| 565 |
+
inline Tensor softshrink(const Tensor& input, double lambda) {
|
| 566 |
+
return torch::softshrink(input, lambda);
|
| 567 |
+
}
|
| 568 |
+
} // namespace detail
|
| 569 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 570 |
+
|
| 571 |
+
/// See
|
| 572 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.softshrink
|
| 573 |
+
/// about the exact behavior of this functional.
|
| 574 |
+
///
|
| 575 |
+
/// See the documentation for `torch::nn::functional::SoftshrinkFuncOptions`
|
| 576 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 577 |
+
///
|
| 578 |
+
/// Example:
|
| 579 |
+
/// ```
|
| 580 |
+
/// namespace F = torch::nn::functional;
|
| 581 |
+
/// F::softshrink(x, F::SoftshrinkFuncOptions(0.42));
|
| 582 |
+
/// ```
|
| 583 |
+
inline Tensor softshrink(
|
| 584 |
+
const Tensor& input,
|
| 585 |
+
const SoftshrinkFuncOptions& options = {}) {
|
| 586 |
+
return detail::softshrink(input, options.lambda());
|
| 587 |
+
}
|
| 588 |
+
|
| 589 |
+
// ============================================================================
|
| 590 |
+
|
| 591 |
+
inline Tensor softsign(const Tensor& input) {
|
| 592 |
+
return input / (input.abs() + 1);
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
+
// ============================================================================
|
| 596 |
+
|
| 597 |
+
inline Tensor tanhshrink(const Tensor& input) {
|
| 598 |
+
return input - input.tanh();
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
// ============================================================================
|
| 602 |
+
|
| 603 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 604 |
+
namespace detail {
|
| 605 |
+
inline Tensor threshold(
|
| 606 |
+
Tensor input,
|
| 607 |
+
double threshold,
|
| 608 |
+
double value,
|
| 609 |
+
bool inplace) {
|
| 610 |
+
if (inplace) {
|
| 611 |
+
return torch::threshold_(input, threshold, value);
|
| 612 |
+
} else {
|
| 613 |
+
return torch::threshold(input, threshold, value);
|
| 614 |
+
}
|
| 615 |
+
}
|
| 616 |
+
} // namespace detail
|
| 617 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 618 |
+
|
| 619 |
+
/// See
|
| 620 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.threshold
|
| 621 |
+
/// about the exact behavior of this functional.
|
| 622 |
+
///
|
| 623 |
+
/// See the documentation for `torch::nn::functional::ThresholdFuncOptions`
|
| 624 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 625 |
+
///
|
| 626 |
+
/// Example:
|
| 627 |
+
/// ```
|
| 628 |
+
/// namespace F = torch::nn::functional;
|
| 629 |
+
/// F::threshold(x, F::ThresholdFuncOptions(0.5, 0.5).inplace(true));
|
| 630 |
+
/// ```
|
| 631 |
+
inline Tensor threshold(Tensor input, const ThresholdFuncOptions& options) {
|
| 632 |
+
return detail::threshold(
|
| 633 |
+
std::move(input),
|
| 634 |
+
options.threshold(),
|
| 635 |
+
options.value(),
|
| 636 |
+
options.inplace());
|
| 637 |
+
}
|
| 638 |
+
|
| 639 |
+
// ============================================================================
|
| 640 |
+
|
| 641 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 642 |
+
namespace detail {
|
| 643 |
+
inline std::tuple<Tensor, Tensor> multi_head_attention_forward(
|
| 644 |
+
const Tensor& query,
|
| 645 |
+
const Tensor& key,
|
| 646 |
+
const Tensor& value,
|
| 647 |
+
int64_t embed_dim_to_check,
|
| 648 |
+
int64_t num_heads,
|
| 649 |
+
const Tensor& in_proj_weight,
|
| 650 |
+
const Tensor& in_proj_bias,
|
| 651 |
+
const Tensor& bias_k,
|
| 652 |
+
const Tensor& bias_v,
|
| 653 |
+
bool add_zero_attn,
|
| 654 |
+
double dropout_p,
|
| 655 |
+
const Tensor& out_proj_weight,
|
| 656 |
+
const Tensor& out_proj_bias,
|
| 657 |
+
bool training = true,
|
| 658 |
+
const Tensor& key_padding_mask = {},
|
| 659 |
+
bool need_weights = true,
|
| 660 |
+
const Tensor& attn_mask = {},
|
| 661 |
+
bool use_separate_proj_weight = false,
|
| 662 |
+
const Tensor& q_proj_weight = {},
|
| 663 |
+
const Tensor& k_proj_weight = {},
|
| 664 |
+
const Tensor& v_proj_weight = {},
|
| 665 |
+
const Tensor& static_k = {},
|
| 666 |
+
const Tensor& static_v = {},
|
| 667 |
+
bool average_attn_weights = true) {
|
| 668 |
+
namespace F = torch::nn::functional;
|
| 669 |
+
|
| 670 |
+
const auto query_sizes = query.sizes();
|
| 671 |
+
const auto& tgt_len = query_sizes[0];
|
| 672 |
+
const auto& bsz = query_sizes[1];
|
| 673 |
+
const auto& embed_dim = query_sizes[2];
|
| 674 |
+
TORCH_INTERNAL_ASSERT(embed_dim == embed_dim_to_check);
|
| 675 |
+
TORCH_INTERNAL_ASSERT(key.sizes() == value.sizes());
|
| 676 |
+
|
| 677 |
+
const auto head_dim = embed_dim / num_heads;
|
| 678 |
+
TORCH_CHECK(
|
| 679 |
+
head_dim * num_heads == embed_dim,
|
| 680 |
+
"embed_dim must be divisible by num_heads");
|
| 681 |
+
const auto scaling = 1 / std::sqrt(head_dim);
|
| 682 |
+
|
| 683 |
+
Tensor q, k, v;
|
| 684 |
+
if (!use_separate_proj_weight) {
|
| 685 |
+
if (torch::equal(query, key) && torch::equal(key, value)) {
|
| 686 |
+
// self-attention
|
| 687 |
+
const auto chunks =
|
| 688 |
+
F::linear(query, in_proj_weight, in_proj_bias).chunk(3, /*dim=*/-1);
|
| 689 |
+
q = chunks[0];
|
| 690 |
+
k = chunks[1];
|
| 691 |
+
v = chunks[2];
|
| 692 |
+
} else if (torch::equal(key, value)) {
|
| 693 |
+
// encoder-decoder attention
|
| 694 |
+
// This is inline in_proj function with in_proj_weight and in_proj_bias
|
| 695 |
+
auto _b = in_proj_bias;
|
| 696 |
+
auto _start = 0;
|
| 697 |
+
auto _end = embed_dim;
|
| 698 |
+
auto _w = in_proj_weight.slice(/*dim=*/0, _start, _end);
|
| 699 |
+
if (_b.defined()) {
|
| 700 |
+
_b = _b.slice(/*dim=*/0, _start, _end);
|
| 701 |
+
}
|
| 702 |
+
q = F::linear(query, _w, _b);
|
| 703 |
+
|
| 704 |
+
if (!key.defined()) {
|
| 705 |
+
TORCH_INTERNAL_ASSERT(!value.defined());
|
| 706 |
+
k.reset();
|
| 707 |
+
v.reset();
|
| 708 |
+
} else {
|
| 709 |
+
// This is inline in_proj function with in_proj_weight and in_proj_bias
|
| 710 |
+
_b = in_proj_bias;
|
| 711 |
+
_start = embed_dim;
|
| 712 |
+
_w = in_proj_weight.slice(/*dim=*/0, _start);
|
| 713 |
+
if (_b.defined()) {
|
| 714 |
+
_b = _b.slice(/*dim=*/0, _start);
|
| 715 |
+
}
|
| 716 |
+
const auto chunks = F::linear(key, _w, _b).chunk(2, /*dim=*/-1);
|
| 717 |
+
k = chunks[0];
|
| 718 |
+
v = chunks[1];
|
| 719 |
+
}
|
| 720 |
+
} else {
|
| 721 |
+
// This is inline in_proj function with in_proj_weight and in_proj_bias
|
| 722 |
+
auto _b = in_proj_bias;
|
| 723 |
+
auto _start = 0;
|
| 724 |
+
auto _end = embed_dim;
|
| 725 |
+
auto _w = in_proj_weight.slice(/*dim=*/0, _start, _end);
|
| 726 |
+
if (_b.defined()) {
|
| 727 |
+
_b = _b.slice(/*dim=*/0, _start, _end);
|
| 728 |
+
}
|
| 729 |
+
q = F::linear(query, _w, _b);
|
| 730 |
+
|
| 731 |
+
// This is inline in_proj function with in_proj_weight and in_proj_bias
|
| 732 |
+
_b = in_proj_bias;
|
| 733 |
+
_start = embed_dim;
|
| 734 |
+
_end = embed_dim * 2;
|
| 735 |
+
_w = in_proj_weight.slice(/*dim=*/0, _start, _end);
|
| 736 |
+
if (_b.defined()) {
|
| 737 |
+
_b = _b.slice(/*dim=*/0, _start, _end);
|
| 738 |
+
}
|
| 739 |
+
k = F::linear(key, _w, _b);
|
| 740 |
+
|
| 741 |
+
// This is inline in_proj function with in_proj_weight and in_proj_bias
|
| 742 |
+
_b = in_proj_bias;
|
| 743 |
+
_start = embed_dim * 2;
|
| 744 |
+
_w = in_proj_weight.slice(/*dim=*/0, _start);
|
| 745 |
+
if (_b.defined()) {
|
| 746 |
+
_b = _b.slice(0, _start);
|
| 747 |
+
}
|
| 748 |
+
v = F::linear(value, _w, _b);
|
| 749 |
+
}
|
| 750 |
+
} else {
|
| 751 |
+
const auto& q_proj_weight_non_opt = q_proj_weight;
|
| 752 |
+
{
|
| 753 |
+
const auto sizes = q_proj_weight_non_opt.sizes();
|
| 754 |
+
const auto len1 = sizes[0];
|
| 755 |
+
const auto len2 = sizes[1];
|
| 756 |
+
TORCH_CHECK(len1 == embed_dim && len2 == query.size(-1));
|
| 757 |
+
}
|
| 758 |
+
|
| 759 |
+
const auto& k_proj_weight_non_opt = k_proj_weight;
|
| 760 |
+
{
|
| 761 |
+
const auto sizes = k_proj_weight_non_opt.sizes();
|
| 762 |
+
const auto len1 = sizes[0];
|
| 763 |
+
const auto len2 = sizes[1];
|
| 764 |
+
TORCH_CHECK(len1 == embed_dim && len2 == key.size(-1));
|
| 765 |
+
}
|
| 766 |
+
|
| 767 |
+
const auto& v_proj_weight_non_opt = v_proj_weight;
|
| 768 |
+
{
|
| 769 |
+
const auto sizes = v_proj_weight_non_opt.sizes();
|
| 770 |
+
const auto len1 = sizes[0];
|
| 771 |
+
const auto len2 = sizes[1];
|
| 772 |
+
TORCH_CHECK(len1 == embed_dim && len2 == value.size(-1));
|
| 773 |
+
}
|
| 774 |
+
|
| 775 |
+
if (in_proj_bias.defined()) {
|
| 776 |
+
q = F::linear(
|
| 777 |
+
query,
|
| 778 |
+
q_proj_weight_non_opt,
|
| 779 |
+
in_proj_bias.slice(/*dim=*/0, 0, embed_dim));
|
| 780 |
+
k = F::linear(
|
| 781 |
+
key,
|
| 782 |
+
k_proj_weight_non_opt,
|
| 783 |
+
in_proj_bias.slice(/*dim=*/0, embed_dim, (embed_dim * 2)));
|
| 784 |
+
v = F::linear(
|
| 785 |
+
value,
|
| 786 |
+
v_proj_weight_non_opt,
|
| 787 |
+
in_proj_bias.slice(/*dim=*/0, (embed_dim * 2)));
|
| 788 |
+
} else {
|
| 789 |
+
q = F::linear(query, q_proj_weight_non_opt, in_proj_bias);
|
| 790 |
+
k = F::linear(key, k_proj_weight_non_opt, in_proj_bias);
|
| 791 |
+
v = F::linear(value, v_proj_weight_non_opt, in_proj_bias);
|
| 792 |
+
}
|
| 793 |
+
}
|
| 794 |
+
q = q * scaling;
|
| 795 |
+
Tensor attn_mask_ = attn_mask;
|
| 796 |
+
Tensor key_padding_mask_ = key_padding_mask;
|
| 797 |
+
if (bias_k.defined() && bias_v.defined()) {
|
| 798 |
+
if (!static_k.defined() && !static_v.defined()) {
|
| 799 |
+
k = torch::cat({k, bias_k.repeat({1, bsz, 1})});
|
| 800 |
+
v = torch::cat({v, bias_v.repeat({1, bsz, 1})});
|
| 801 |
+
if (attn_mask_.defined()) {
|
| 802 |
+
attn_mask_ = torch::cat(
|
| 803 |
+
{attn_mask_,
|
| 804 |
+
torch::zeros(
|
| 805 |
+
{attn_mask_.size(0), 1},
|
| 806 |
+
at::TensorOptions(attn_mask_.dtype())
|
| 807 |
+
.device(attn_mask_.device()))},
|
| 808 |
+
/*dim=*/1);
|
| 809 |
+
}
|
| 810 |
+
if (key_padding_mask_.defined()) {
|
| 811 |
+
key_padding_mask_ = torch::cat(
|
| 812 |
+
{key_padding_mask_,
|
| 813 |
+
torch::zeros(
|
| 814 |
+
{key_padding_mask_.size(0), 1},
|
| 815 |
+
at::TensorOptions(key_padding_mask_.dtype())
|
| 816 |
+
.device(key_padding_mask_.device()))},
|
| 817 |
+
/*dim=*/1);
|
| 818 |
+
}
|
| 819 |
+
} else {
|
| 820 |
+
TORCH_CHECK(!static_k.defined(), "bias cannot be added to static key.");
|
| 821 |
+
TORCH_CHECK(!static_v.defined(), "bias cannot be added to static value.");
|
| 822 |
+
}
|
| 823 |
+
} else {
|
| 824 |
+
TORCH_CHECK(!bias_k.defined());
|
| 825 |
+
TORCH_CHECK(!bias_v.defined());
|
| 826 |
+
}
|
| 827 |
+
q = q.contiguous().view({tgt_len, bsz * num_heads, head_dim}).transpose(0, 1);
|
| 828 |
+
if (k.defined()) {
|
| 829 |
+
k = k.contiguous().view({-1, bsz * num_heads, head_dim}).transpose(0, 1);
|
| 830 |
+
}
|
| 831 |
+
if (v.defined()) {
|
| 832 |
+
v = v.contiguous().view({-1, bsz * num_heads, head_dim}).transpose(0, 1);
|
| 833 |
+
}
|
| 834 |
+
if (static_k.defined()) {
|
| 835 |
+
TORCH_CHECK(static_k.size(0) == bsz * num_heads);
|
| 836 |
+
TORCH_CHECK(static_k.size(2) == head_dim);
|
| 837 |
+
k = static_k;
|
| 838 |
+
}
|
| 839 |
+
if (static_v.defined()) {
|
| 840 |
+
TORCH_CHECK(static_v.size(0) == bsz * num_heads);
|
| 841 |
+
TORCH_CHECK(static_v.size(2) == head_dim);
|
| 842 |
+
v = static_v;
|
| 843 |
+
}
|
| 844 |
+
auto src_len = k.size(1);
|
| 845 |
+
if (key_padding_mask_.defined()) {
|
| 846 |
+
TORCH_CHECK(key_padding_mask_.size(0) == bsz);
|
| 847 |
+
TORCH_CHECK(key_padding_mask_.size(1) == src_len);
|
| 848 |
+
}
|
| 849 |
+
if (add_zero_attn) {
|
| 850 |
+
src_len += 1;
|
| 851 |
+
auto k_sizes = k.sizes().vec();
|
| 852 |
+
k_sizes[1] = 1;
|
| 853 |
+
k = torch::cat(
|
| 854 |
+
{k,
|
| 855 |
+
torch::zeros(
|
| 856 |
+
k_sizes, at::TensorOptions(k.dtype()).device(k.device()))},
|
| 857 |
+
/*dim=*/1);
|
| 858 |
+
auto v_sizes = v.sizes().vec();
|
| 859 |
+
v_sizes[1] = 1;
|
| 860 |
+
v = torch::cat(
|
| 861 |
+
{v,
|
| 862 |
+
torch::zeros(
|
| 863 |
+
v_sizes, at::TensorOptions(v.dtype()).device(v.device()))},
|
| 864 |
+
/*dim=*/1);
|
| 865 |
+
if (attn_mask_.defined()) {
|
| 866 |
+
attn_mask_ = torch::cat(
|
| 867 |
+
{attn_mask_,
|
| 868 |
+
torch::zeros(
|
| 869 |
+
{attn_mask_.size(0), 1},
|
| 870 |
+
at::TensorOptions(attn_mask_.dtype())
|
| 871 |
+
.device(attn_mask_.device()))},
|
| 872 |
+
/*dim=*/1);
|
| 873 |
+
}
|
| 874 |
+
if (key_padding_mask_.defined()) {
|
| 875 |
+
key_padding_mask_ = torch::cat(
|
| 876 |
+
{key_padding_mask_,
|
| 877 |
+
torch::zeros(
|
| 878 |
+
{key_padding_mask_.size(0), 1},
|
| 879 |
+
at::TensorOptions(key_padding_mask_.dtype())
|
| 880 |
+
.device(key_padding_mask_.device()))},
|
| 881 |
+
/*dim=*/1);
|
| 882 |
+
}
|
| 883 |
+
}
|
| 884 |
+
auto attn_output_weights = torch::bmm(q, k.transpose(1, 2));
|
| 885 |
+
TORCH_CHECK(
|
| 886 |
+
attn_output_weights.sizes() ==
|
| 887 |
+
IntArrayRef({bsz * num_heads, tgt_len, src_len}));
|
| 888 |
+
if (attn_mask_.defined()) {
|
| 889 |
+
attn_mask_ = attn_mask_.unsqueeze(0);
|
| 890 |
+
attn_output_weights += attn_mask_;
|
| 891 |
+
}
|
| 892 |
+
if (key_padding_mask_.defined()) {
|
| 893 |
+
attn_output_weights =
|
| 894 |
+
attn_output_weights.view({bsz, num_heads, tgt_len, src_len});
|
| 895 |
+
attn_output_weights = AT_DISPATCH_FLOATING_TYPES(
|
| 896 |
+
attn_output_weights.scalar_type(),
|
| 897 |
+
"attn_output_weights.masked_fill",
|
| 898 |
+
[&]() {
|
| 899 |
+
return attn_output_weights.masked_fill(
|
| 900 |
+
key_padding_mask_.unsqueeze(1).unsqueeze(2),
|
| 901 |
+
-std::numeric_limits<scalar_t>::infinity());
|
| 902 |
+
});
|
| 903 |
+
attn_output_weights =
|
| 904 |
+
attn_output_weights.view({bsz * num_heads, tgt_len, src_len});
|
| 905 |
+
}
|
| 906 |
+
// NOLINTNEXTLINE(bugprone-argument-comment)
|
| 907 |
+
attn_output_weights = F::softmax(attn_output_weights, /*dim=*/-1);
|
| 908 |
+
attn_output_weights = F::dropout(
|
| 909 |
+
attn_output_weights,
|
| 910 |
+
F::DropoutFuncOptions().p(dropout_p).training(training));
|
| 911 |
+
auto attn_output = torch::bmm(attn_output_weights, v);
|
| 912 |
+
TORCH_CHECK(
|
| 913 |
+
attn_output.sizes() == IntArrayRef({bsz * num_heads, tgt_len, head_dim}));
|
| 914 |
+
attn_output =
|
| 915 |
+
attn_output.transpose(0, 1).contiguous().view({tgt_len, bsz, embed_dim});
|
| 916 |
+
attn_output = F::linear(attn_output, out_proj_weight, out_proj_bias);
|
| 917 |
+
if (need_weights) {
|
| 918 |
+
attn_output_weights =
|
| 919 |
+
attn_output_weights.view({bsz, num_heads, tgt_len, src_len});
|
| 920 |
+
if (average_attn_weights) {
|
| 921 |
+
// average attention weights over heads
|
| 922 |
+
attn_output_weights = attn_output_weights.sum(/*dim=*/1) / num_heads;
|
| 923 |
+
}
|
| 924 |
+
return std::make_tuple(attn_output, attn_output_weights);
|
| 925 |
+
} else {
|
| 926 |
+
return std::make_tuple(attn_output, Tensor());
|
| 927 |
+
}
|
| 928 |
+
}
|
| 929 |
+
} // namespace detail
|
| 930 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 931 |
+
|
| 932 |
+
inline std::tuple<Tensor, Tensor> multi_head_attention_forward(
|
| 933 |
+
const Tensor& query,
|
| 934 |
+
const Tensor& key,
|
| 935 |
+
const Tensor& value,
|
| 936 |
+
const MultiheadAttentionForwardFuncOptions& options) {
|
| 937 |
+
return detail::multi_head_attention_forward(
|
| 938 |
+
query,
|
| 939 |
+
key,
|
| 940 |
+
value,
|
| 941 |
+
options.embed_dim_to_check(),
|
| 942 |
+
options.num_heads(),
|
| 943 |
+
options.in_proj_weight(),
|
| 944 |
+
options.in_proj_bias(),
|
| 945 |
+
options.bias_k(),
|
| 946 |
+
options.bias_v(),
|
| 947 |
+
options.add_zero_attn(),
|
| 948 |
+
options.dropout_p(),
|
| 949 |
+
options.out_proj_weight(),
|
| 950 |
+
options.out_proj_bias(),
|
| 951 |
+
options.training(),
|
| 952 |
+
options.key_padding_mask(),
|
| 953 |
+
options.need_weights(),
|
| 954 |
+
options.attn_mask(),
|
| 955 |
+
options.use_separate_proj_weight(),
|
| 956 |
+
options.q_proj_weight(),
|
| 957 |
+
options.k_proj_weight(),
|
| 958 |
+
options.v_proj_weight(),
|
| 959 |
+
options.static_k(),
|
| 960 |
+
options.static_v(),
|
| 961 |
+
options.average_attn_weights());
|
| 962 |
+
}
|
| 963 |
+
|
| 964 |
+
} // namespace functional
|
| 965 |
+
} // namespace nn
|
| 966 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/batchnorm.h
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <c10/util/irange.h>
|
| 4 |
+
#include <torch/nn/options/batchnorm.h>
|
| 5 |
+
#include <torch/types.h>
|
| 6 |
+
|
| 7 |
+
namespace torch {
|
| 8 |
+
namespace nn {
|
| 9 |
+
namespace functional {
|
| 10 |
+
|
| 11 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 12 |
+
namespace detail {
|
| 13 |
+
inline Tensor batch_norm(
|
| 14 |
+
const Tensor& input,
|
| 15 |
+
const Tensor& running_mean,
|
| 16 |
+
const Tensor& running_var,
|
| 17 |
+
Tensor weight,
|
| 18 |
+
Tensor bias,
|
| 19 |
+
bool training,
|
| 20 |
+
c10::optional<double> momentum,
|
| 21 |
+
double eps) {
|
| 22 |
+
TORCH_CHECK(
|
| 23 |
+
input.dim() >= 2,
|
| 24 |
+
"Expected at least 2 input dimensions, but got ",
|
| 25 |
+
input.dim());
|
| 26 |
+
if (training) {
|
| 27 |
+
auto size = input.sizes();
|
| 28 |
+
int64_t size_prods = size[0];
|
| 29 |
+
for (const auto i : c10::irange(size.size() - 2)) {
|
| 30 |
+
size_prods *= size[i + 2];
|
| 31 |
+
}
|
| 32 |
+
TORCH_CHECK(
|
| 33 |
+
size_prods != 1,
|
| 34 |
+
"Expected more than 1 value per channel when training, got input size ",
|
| 35 |
+
size);
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return torch::batch_norm(
|
| 39 |
+
input,
|
| 40 |
+
weight,
|
| 41 |
+
bias,
|
| 42 |
+
running_mean,
|
| 43 |
+
running_var,
|
| 44 |
+
training,
|
| 45 |
+
momentum.value(),
|
| 46 |
+
eps,
|
| 47 |
+
at::globalContext().userEnabledCuDNN());
|
| 48 |
+
}
|
| 49 |
+
} // namespace detail
|
| 50 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 51 |
+
|
| 52 |
+
/// See
|
| 53 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.batch_norm
|
| 54 |
+
/// about the exact behavior of this functional.
|
| 55 |
+
///
|
| 56 |
+
/// See the documentation for `torch::nn::functional::BatchNormFuncOptions`
|
| 57 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 58 |
+
///
|
| 59 |
+
/// Example:
|
| 60 |
+
/// ```
|
| 61 |
+
/// namespace F = torch::nn::functional;
|
| 62 |
+
/// F::batch_norm(input, mean, variance,
|
| 63 |
+
/// F::BatchNormFuncOptions().weight(weight).bias(bias).momentum(0.1).eps(1e-05).training(false));
|
| 64 |
+
/// ```
|
| 65 |
+
inline Tensor batch_norm(
|
| 66 |
+
const Tensor& input,
|
| 67 |
+
const Tensor& running_mean,
|
| 68 |
+
const Tensor& running_var,
|
| 69 |
+
const BatchNormFuncOptions& options = {}) {
|
| 70 |
+
return detail::batch_norm(
|
| 71 |
+
input,
|
| 72 |
+
running_mean,
|
| 73 |
+
running_var,
|
| 74 |
+
options.weight(),
|
| 75 |
+
options.bias(),
|
| 76 |
+
options.training(),
|
| 77 |
+
options.momentum(),
|
| 78 |
+
options.eps());
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
} // namespace functional
|
| 82 |
+
} // namespace nn
|
| 83 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/conv.h
ADDED
|
@@ -0,0 +1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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/options/conv.h>
|
| 4 |
+
#include <torch/types.h>
|
| 5 |
+
|
| 6 |
+
namespace torch {
|
| 7 |
+
namespace nn {
|
| 8 |
+
namespace functional {
|
| 9 |
+
|
| 10 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 11 |
+
namespace detail {
|
| 12 |
+
|
| 13 |
+
inline std::string padding_unwrap(enumtype::kValid) {
|
| 14 |
+
return "valid";
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
inline std::string padding_unwrap(enumtype::kSame) {
|
| 18 |
+
return "same";
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
template <size_t D>
|
| 22 |
+
IntArrayRef padding_unwrap(const ExpandingArray<D>& array) {
|
| 23 |
+
return array;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
inline Tensor conv1d(
|
| 27 |
+
const Tensor& input,
|
| 28 |
+
const Tensor& weight,
|
| 29 |
+
const Tensor& bias,
|
| 30 |
+
ExpandingArray<1> stride,
|
| 31 |
+
const Conv1dFuncOptions::padding_t& padding,
|
| 32 |
+
ExpandingArray<1> dilation,
|
| 33 |
+
int64_t groups) {
|
| 34 |
+
return std::visit(
|
| 35 |
+
[&](const auto& pad) {
|
| 36 |
+
return torch::conv1d(
|
| 37 |
+
input, weight, bias, stride, padding_unwrap(pad), dilation, groups);
|
| 38 |
+
},
|
| 39 |
+
padding);
|
| 40 |
+
}
|
| 41 |
+
} // namespace detail
|
| 42 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 43 |
+
|
| 44 |
+
/// See
|
| 45 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.conv1d
|
| 46 |
+
/// about the exact behavior of this functional.
|
| 47 |
+
///
|
| 48 |
+
/// See the documentation for `torch::nn::functional::Conv1dFuncOptions` class
|
| 49 |
+
/// to learn what optional arguments are supported for this functional.
|
| 50 |
+
///
|
| 51 |
+
/// Example:
|
| 52 |
+
/// ```
|
| 53 |
+
/// namespace F = torch::nn::functional;
|
| 54 |
+
/// F::conv1d(x, weight, F::Conv1dFuncOptions().stride(1));
|
| 55 |
+
/// ```
|
| 56 |
+
inline Tensor conv1d(
|
| 57 |
+
const Tensor& input,
|
| 58 |
+
const Tensor& weight,
|
| 59 |
+
const Conv1dFuncOptions& options = {}) {
|
| 60 |
+
return detail::conv1d(
|
| 61 |
+
input,
|
| 62 |
+
weight,
|
| 63 |
+
options.bias(),
|
| 64 |
+
options.stride(),
|
| 65 |
+
options.padding(),
|
| 66 |
+
options.dilation(),
|
| 67 |
+
options.groups());
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 71 |
+
namespace detail {
|
| 72 |
+
inline Tensor conv2d(
|
| 73 |
+
const Tensor& input,
|
| 74 |
+
const Tensor& weight,
|
| 75 |
+
const Tensor& bias,
|
| 76 |
+
ExpandingArray<2> stride,
|
| 77 |
+
const Conv2dFuncOptions::padding_t& padding,
|
| 78 |
+
ExpandingArray<2> dilation,
|
| 79 |
+
int64_t groups) {
|
| 80 |
+
return std::visit(
|
| 81 |
+
[&](const auto& pad) {
|
| 82 |
+
return torch::conv2d(
|
| 83 |
+
input, weight, bias, stride, padding_unwrap(pad), dilation, groups);
|
| 84 |
+
},
|
| 85 |
+
padding);
|
| 86 |
+
}
|
| 87 |
+
} // namespace detail
|
| 88 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 89 |
+
|
| 90 |
+
/// See
|
| 91 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.conv2d
|
| 92 |
+
/// about the exact behavior of this functional.
|
| 93 |
+
///
|
| 94 |
+
/// See the documentation for `torch::nn::functional::Conv2dFuncOptions` class
|
| 95 |
+
/// to learn what optional arguments are supported for this functional.
|
| 96 |
+
///
|
| 97 |
+
/// Example:
|
| 98 |
+
/// ```
|
| 99 |
+
/// namespace F = torch::nn::functional;
|
| 100 |
+
/// F::conv2d(x, weight, F::Conv2dFuncOptions().stride(1));
|
| 101 |
+
/// ```
|
| 102 |
+
inline Tensor conv2d(
|
| 103 |
+
const Tensor& input,
|
| 104 |
+
const Tensor& weight,
|
| 105 |
+
const Conv2dFuncOptions& options = {}) {
|
| 106 |
+
return detail::conv2d(
|
| 107 |
+
input,
|
| 108 |
+
weight,
|
| 109 |
+
options.bias(),
|
| 110 |
+
options.stride(),
|
| 111 |
+
options.padding(),
|
| 112 |
+
options.dilation(),
|
| 113 |
+
options.groups());
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 117 |
+
namespace detail {
|
| 118 |
+
inline Tensor conv3d(
|
| 119 |
+
const Tensor& input,
|
| 120 |
+
const Tensor& weight,
|
| 121 |
+
const Tensor& bias,
|
| 122 |
+
ExpandingArray<3> stride,
|
| 123 |
+
const Conv3dFuncOptions::padding_t& padding,
|
| 124 |
+
ExpandingArray<3> dilation,
|
| 125 |
+
int64_t groups) {
|
| 126 |
+
return std::visit(
|
| 127 |
+
[&](const auto& pad) {
|
| 128 |
+
return torch::conv3d(
|
| 129 |
+
input, weight, bias, stride, padding_unwrap(pad), dilation, groups);
|
| 130 |
+
},
|
| 131 |
+
padding);
|
| 132 |
+
}
|
| 133 |
+
} // namespace detail
|
| 134 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 135 |
+
|
| 136 |
+
/// See
|
| 137 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.conv3d
|
| 138 |
+
/// about the exact behavior of this functional.
|
| 139 |
+
///
|
| 140 |
+
/// See the documentation for `torch::nn::functional::Conv3dFuncOptions` class
|
| 141 |
+
/// to learn what optional arguments are supported for this functional.
|
| 142 |
+
///
|
| 143 |
+
/// Example:
|
| 144 |
+
/// ```
|
| 145 |
+
/// namespace F = torch::nn::functional;
|
| 146 |
+
/// F::conv3d(x, weight, F::Conv3dFuncOptions().stride(1));
|
| 147 |
+
/// ```
|
| 148 |
+
inline Tensor conv3d(
|
| 149 |
+
const Tensor& input,
|
| 150 |
+
const Tensor& weight,
|
| 151 |
+
const Conv3dFuncOptions& options = {}) {
|
| 152 |
+
return detail::conv3d(
|
| 153 |
+
input,
|
| 154 |
+
weight,
|
| 155 |
+
options.bias(),
|
| 156 |
+
options.stride(),
|
| 157 |
+
options.padding(),
|
| 158 |
+
options.dilation(),
|
| 159 |
+
options.groups());
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
// ============================================================================
|
| 163 |
+
|
| 164 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 165 |
+
namespace detail {
|
| 166 |
+
inline Tensor conv_transpose1d(
|
| 167 |
+
const Tensor& input,
|
| 168 |
+
const Tensor& weight,
|
| 169 |
+
const Tensor& bias,
|
| 170 |
+
IntArrayRef stride,
|
| 171 |
+
IntArrayRef padding,
|
| 172 |
+
IntArrayRef output_padding,
|
| 173 |
+
int64_t groups,
|
| 174 |
+
IntArrayRef dilation) {
|
| 175 |
+
return torch::conv_transpose1d(
|
| 176 |
+
input, weight, bias, stride, padding, output_padding, groups, dilation);
|
| 177 |
+
}
|
| 178 |
+
} // namespace detail
|
| 179 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 180 |
+
|
| 181 |
+
/// See
|
| 182 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.conv_transpose1d
|
| 183 |
+
/// about the exact behavior of this functional.
|
| 184 |
+
///
|
| 185 |
+
/// See the documentation for
|
| 186 |
+
/// `torch::nn::functional::ConvTranspose1dFuncOptions` class to learn what
|
| 187 |
+
/// optional arguments are supported for this functional.
|
| 188 |
+
///
|
| 189 |
+
/// Example:
|
| 190 |
+
/// ```
|
| 191 |
+
/// namespace F = torch::nn::functional;
|
| 192 |
+
/// F::conv_transpose1d(x, weight, F::ConvTranspose1dFuncOptions().stride(1));
|
| 193 |
+
/// ```
|
| 194 |
+
inline Tensor conv_transpose1d(
|
| 195 |
+
const Tensor& input,
|
| 196 |
+
const Tensor& weight,
|
| 197 |
+
const ConvTranspose1dFuncOptions& options = {}) {
|
| 198 |
+
return detail::conv_transpose1d(
|
| 199 |
+
input,
|
| 200 |
+
weight,
|
| 201 |
+
options.bias(),
|
| 202 |
+
options.stride(),
|
| 203 |
+
options.padding(),
|
| 204 |
+
options.output_padding(),
|
| 205 |
+
options.groups(),
|
| 206 |
+
options.dilation());
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 210 |
+
namespace detail {
|
| 211 |
+
inline Tensor conv_transpose2d(
|
| 212 |
+
const Tensor& input,
|
| 213 |
+
const Tensor& weight,
|
| 214 |
+
const Tensor& bias,
|
| 215 |
+
IntArrayRef stride,
|
| 216 |
+
IntArrayRef padding,
|
| 217 |
+
IntArrayRef output_padding,
|
| 218 |
+
int64_t groups,
|
| 219 |
+
IntArrayRef dilation) {
|
| 220 |
+
return torch::conv_transpose2d(
|
| 221 |
+
input, weight, bias, stride, padding, output_padding, groups, dilation);
|
| 222 |
+
}
|
| 223 |
+
} // namespace detail
|
| 224 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 225 |
+
|
| 226 |
+
/// See
|
| 227 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.conv_transpose2d
|
| 228 |
+
/// about the exact behavior of this functional.
|
| 229 |
+
///
|
| 230 |
+
/// See the documentation for
|
| 231 |
+
/// `torch::nn::functional::ConvTranspose2dFuncOptions` class to learn what
|
| 232 |
+
/// optional arguments are supported for this functional.
|
| 233 |
+
///
|
| 234 |
+
/// Example:
|
| 235 |
+
/// ```
|
| 236 |
+
/// namespace F = torch::nn::functional;
|
| 237 |
+
/// F::conv_transpose2d(x, weight, F::ConvTranspose2dFuncOptions().stride(1));
|
| 238 |
+
/// ```
|
| 239 |
+
inline Tensor conv_transpose2d(
|
| 240 |
+
const Tensor& input,
|
| 241 |
+
const Tensor& weight,
|
| 242 |
+
const ConvTranspose2dFuncOptions& options = {}) {
|
| 243 |
+
return detail::conv_transpose2d(
|
| 244 |
+
input,
|
| 245 |
+
weight,
|
| 246 |
+
options.bias(),
|
| 247 |
+
options.stride(),
|
| 248 |
+
options.padding(),
|
| 249 |
+
options.output_padding(),
|
| 250 |
+
options.groups(),
|
| 251 |
+
options.dilation());
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 255 |
+
namespace detail {
|
| 256 |
+
inline Tensor conv_transpose3d(
|
| 257 |
+
const Tensor& input,
|
| 258 |
+
const Tensor& weight,
|
| 259 |
+
const Tensor& bias,
|
| 260 |
+
IntArrayRef stride,
|
| 261 |
+
IntArrayRef padding,
|
| 262 |
+
IntArrayRef output_padding,
|
| 263 |
+
int64_t groups,
|
| 264 |
+
IntArrayRef dilation) {
|
| 265 |
+
return torch::conv_transpose3d(
|
| 266 |
+
input, weight, bias, stride, padding, output_padding, groups, dilation);
|
| 267 |
+
}
|
| 268 |
+
} // namespace detail
|
| 269 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 270 |
+
|
| 271 |
+
/// See
|
| 272 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.conv_transpose3d
|
| 273 |
+
/// about the exact behavior of this functional.
|
| 274 |
+
///
|
| 275 |
+
/// See the documentation for
|
| 276 |
+
/// `torch::nn::functional::ConvTranspose3dFuncOptions` class to learn what
|
| 277 |
+
/// optional arguments are supported for this functional.
|
| 278 |
+
///
|
| 279 |
+
/// Example:
|
| 280 |
+
/// ```
|
| 281 |
+
/// namespace F = torch::nn::functional;
|
| 282 |
+
/// F::conv_transpose3d(x, weight, F::ConvTranspose3dFuncOptions().stride(1));
|
| 283 |
+
/// ```
|
| 284 |
+
inline Tensor conv_transpose3d(
|
| 285 |
+
const Tensor& input,
|
| 286 |
+
const Tensor& weight,
|
| 287 |
+
const ConvTranspose3dFuncOptions& options = {}) {
|
| 288 |
+
return detail::conv_transpose3d(
|
| 289 |
+
input,
|
| 290 |
+
weight,
|
| 291 |
+
options.bias(),
|
| 292 |
+
options.stride(),
|
| 293 |
+
options.padding(),
|
| 294 |
+
options.output_padding(),
|
| 295 |
+
options.groups(),
|
| 296 |
+
options.dilation());
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
} // namespace functional
|
| 300 |
+
} // namespace nn
|
| 301 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/distance.h
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/options/distance.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace nn {
|
| 7 |
+
namespace functional {
|
| 8 |
+
|
| 9 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 10 |
+
namespace detail {
|
| 11 |
+
inline Tensor cosine_similarity(
|
| 12 |
+
const Tensor& x1,
|
| 13 |
+
const Tensor& x2,
|
| 14 |
+
int64_t dim,
|
| 15 |
+
double eps) {
|
| 16 |
+
return torch::cosine_similarity(x1, x2, dim, eps);
|
| 17 |
+
}
|
| 18 |
+
} // namespace detail
|
| 19 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 20 |
+
|
| 21 |
+
/// See
|
| 22 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.cosine_similarity
|
| 23 |
+
/// about the exact behavior of this functional.
|
| 24 |
+
///
|
| 25 |
+
/// See the documentation for
|
| 26 |
+
/// `torch::nn::functional::CosineSimilarityFuncOptions` class to learn what
|
| 27 |
+
/// optional arguments are supported for this functional.
|
| 28 |
+
///
|
| 29 |
+
/// Example:
|
| 30 |
+
/// ```
|
| 31 |
+
/// namespace F = torch::nn::functional;
|
| 32 |
+
/// F::cosine_similarity(input1, input2,
|
| 33 |
+
/// F::CosineSimilarityFuncOptions().dim(1));
|
| 34 |
+
/// ```
|
| 35 |
+
inline Tensor cosine_similarity(
|
| 36 |
+
const Tensor& x1,
|
| 37 |
+
const Tensor& x2,
|
| 38 |
+
const CosineSimilarityFuncOptions& options = {}) {
|
| 39 |
+
return detail::cosine_similarity(x1, x2, options.dim(), options.eps());
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
// ============================================================================
|
| 43 |
+
|
| 44 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 45 |
+
namespace detail {
|
| 46 |
+
inline Tensor pairwise_distance(
|
| 47 |
+
const Tensor& x1,
|
| 48 |
+
const Tensor& x2,
|
| 49 |
+
double p,
|
| 50 |
+
double eps,
|
| 51 |
+
bool keepdim) {
|
| 52 |
+
return torch::pairwise_distance(x1, x2, p, eps, keepdim);
|
| 53 |
+
}
|
| 54 |
+
} // namespace detail
|
| 55 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 56 |
+
|
| 57 |
+
/// See
|
| 58 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.pairwise_distance
|
| 59 |
+
/// about the exact behavior of this functional.
|
| 60 |
+
///
|
| 61 |
+
/// See the documentation for
|
| 62 |
+
/// `torch::nn::functional::PairwiseDistanceFuncOptions` class to learn what
|
| 63 |
+
/// optional arguments are supported for this functional.
|
| 64 |
+
///
|
| 65 |
+
/// Example:
|
| 66 |
+
/// ```
|
| 67 |
+
/// namespace F = torch::nn::functional;
|
| 68 |
+
/// F::pairwise_distance(input1, input2, F::PairwiseDistanceFuncOptions().p(1));
|
| 69 |
+
/// ```
|
| 70 |
+
inline Tensor pairwise_distance(
|
| 71 |
+
const Tensor& x1,
|
| 72 |
+
const Tensor& x2,
|
| 73 |
+
const PairwiseDistanceFuncOptions& options = {}) {
|
| 74 |
+
return detail::pairwise_distance(
|
| 75 |
+
x1, x2, options.p(), options.eps(), options.keepdim());
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// ============================================================================
|
| 79 |
+
|
| 80 |
+
/// Computes the p-norm distance between every pair of row vectors in the input.
|
| 81 |
+
/// This function will be faster if the rows are contiguous.
|
| 82 |
+
inline Tensor pdist(const Tensor& input, double p = 2.0) {
|
| 83 |
+
return torch::pdist(input, p);
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
} // namespace functional
|
| 87 |
+
} // namespace nn
|
| 88 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/dropout.h
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
#include <torch/nn/options/dropout.h>
|
| 4 |
+
|
| 5 |
+
#include <utility>
|
| 6 |
+
|
| 7 |
+
namespace torch {
|
| 8 |
+
namespace nn {
|
| 9 |
+
namespace functional {
|
| 10 |
+
|
| 11 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 12 |
+
namespace detail {
|
| 13 |
+
|
| 14 |
+
inline Tensor dropout(Tensor input, double p, bool training, bool inplace) {
|
| 15 |
+
TORCH_CHECK(
|
| 16 |
+
p >= 0. && p <= 1.,
|
| 17 |
+
"dropout probability has to be between 0 and 1, but got ",
|
| 18 |
+
p);
|
| 19 |
+
if (inplace) {
|
| 20 |
+
return torch::dropout_(input, p, training);
|
| 21 |
+
} else {
|
| 22 |
+
return torch::dropout(input, p, training);
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
} // namespace detail
|
| 27 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 28 |
+
|
| 29 |
+
/// See
|
| 30 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.dropout
|
| 31 |
+
/// about the exact behavior of this functional.
|
| 32 |
+
///
|
| 33 |
+
/// See the documentation for `torch::nn::functional::DropoutFuncOptions` class
|
| 34 |
+
/// to learn what optional arguments are supported for this functional.
|
| 35 |
+
///
|
| 36 |
+
/// Example:
|
| 37 |
+
/// ```
|
| 38 |
+
/// namespace F = torch::nn::functional;
|
| 39 |
+
/// F::dropout(input, F::DropoutFuncOptions().p(0.5));
|
| 40 |
+
/// ```
|
| 41 |
+
inline Tensor dropout(Tensor input, const DropoutFuncOptions& options = {}) {
|
| 42 |
+
return detail::dropout(
|
| 43 |
+
std::move(input), options.p(), options.training(), options.inplace());
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// ============================================================================
|
| 47 |
+
|
| 48 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 49 |
+
namespace detail {
|
| 50 |
+
|
| 51 |
+
template <int64_t unbatched_dim, int64_t batched_dim>
|
| 52 |
+
inline Tensor _dropoutNd_helper(
|
| 53 |
+
Tensor input,
|
| 54 |
+
double p,
|
| 55 |
+
bool training,
|
| 56 |
+
bool inplace,
|
| 57 |
+
const char* fn_name) {
|
| 58 |
+
TORCH_CHECK(
|
| 59 |
+
p >= 0. && p <= 1.,
|
| 60 |
+
"dropout probability has to be between 0 and 1, but got ",
|
| 61 |
+
p);
|
| 62 |
+
|
| 63 |
+
auto inp_dim = input.dim();
|
| 64 |
+
auto is_batched = inp_dim == batched_dim;
|
| 65 |
+
if (!is_batched) {
|
| 66 |
+
if (inplace) {
|
| 67 |
+
input = input.unsqueeze_(0);
|
| 68 |
+
} else {
|
| 69 |
+
input = input.unsqueeze(0);
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
Tensor result;
|
| 74 |
+
if (inplace) {
|
| 75 |
+
result = torch::feature_dropout_(input, p, training);
|
| 76 |
+
} else {
|
| 77 |
+
result = torch::feature_dropout(input, p, training);
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
if (!is_batched) {
|
| 81 |
+
if (inplace) {
|
| 82 |
+
result = result.squeeze_(0);
|
| 83 |
+
} else {
|
| 84 |
+
result = result.squeeze(0);
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
return result;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
inline Tensor dropout2d(Tensor input, double p, bool training, bool inplace) {
|
| 91 |
+
return _dropoutNd_helper<3, 4>(
|
| 92 |
+
std::move(input), p, training, inplace, "dropout2d");
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
} // namespace detail
|
| 96 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 97 |
+
|
| 98 |
+
/// See
|
| 99 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.dropout2d
|
| 100 |
+
/// about the exact behavior of this functional.
|
| 101 |
+
///
|
| 102 |
+
/// See the documentation for `torch::nn::functional::Dropout2dFuncOptions`
|
| 103 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 104 |
+
///
|
| 105 |
+
/// Example:
|
| 106 |
+
/// ```
|
| 107 |
+
/// namespace F = torch::nn::functional;
|
| 108 |
+
/// F::dropout2d(input, F::Dropout2dFuncOptions().p(0.5));
|
| 109 |
+
/// ```
|
| 110 |
+
inline Tensor dropout2d(
|
| 111 |
+
Tensor input,
|
| 112 |
+
const Dropout2dFuncOptions& options = {}) {
|
| 113 |
+
return detail::dropout2d(
|
| 114 |
+
std::move(input), options.p(), options.training(), options.inplace());
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// ============================================================================
|
| 118 |
+
|
| 119 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 120 |
+
namespace detail {
|
| 121 |
+
|
| 122 |
+
inline Tensor dropout3d(Tensor input, double p, bool training, bool inplace) {
|
| 123 |
+
return _dropoutNd_helper<4, 5>(
|
| 124 |
+
std::move(input), p, training, inplace, "dropout3d");
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
} // namespace detail
|
| 128 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 129 |
+
|
| 130 |
+
/// See
|
| 131 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.dropout3d
|
| 132 |
+
/// about the exact behavior of this functional.
|
| 133 |
+
///
|
| 134 |
+
/// See the documentation for `torch::nn::functional::Dropout3dFuncOptions`
|
| 135 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 136 |
+
///
|
| 137 |
+
/// Example:
|
| 138 |
+
/// ```
|
| 139 |
+
/// namespace F = torch::nn::functional;
|
| 140 |
+
/// F::dropout3d(input, F::Dropout3dFuncOptions().p(0.5));
|
| 141 |
+
/// ```
|
| 142 |
+
inline Tensor dropout3d(
|
| 143 |
+
Tensor input,
|
| 144 |
+
const Dropout3dFuncOptions& options = {}) {
|
| 145 |
+
return detail::dropout3d(
|
| 146 |
+
std::move(input), options.p(), options.training(), options.inplace());
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// ============================================================================
|
| 150 |
+
|
| 151 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 152 |
+
namespace detail {
|
| 153 |
+
|
| 154 |
+
inline Tensor alpha_dropout(
|
| 155 |
+
Tensor input,
|
| 156 |
+
double p,
|
| 157 |
+
bool training,
|
| 158 |
+
bool inplace) {
|
| 159 |
+
if (p < 0. || p > 1.) {
|
| 160 |
+
TORCH_CHECK(
|
| 161 |
+
false, "dropout probability has to be between 0 and 1, but got ", p);
|
| 162 |
+
}
|
| 163 |
+
return inplace ? torch::alpha_dropout_(input, p, training)
|
| 164 |
+
: torch::alpha_dropout(input, p, training);
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
} // namespace detail
|
| 168 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 169 |
+
|
| 170 |
+
/// See
|
| 171 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.alpha_dropout
|
| 172 |
+
/// about the exact behavior of this functional.
|
| 173 |
+
///
|
| 174 |
+
/// See the documentation for `torch::nn::functional::AlphaDropoutFuncOptions`
|
| 175 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 176 |
+
///
|
| 177 |
+
/// Example:
|
| 178 |
+
/// ```
|
| 179 |
+
/// namespace F = torch::nn::functional;
|
| 180 |
+
/// F::alpha_dropout(input,
|
| 181 |
+
/// F::AlphaDropoutFuncOptions().p(0.5).training(false));
|
| 182 |
+
/// ```
|
| 183 |
+
inline Tensor alpha_dropout(
|
| 184 |
+
Tensor input,
|
| 185 |
+
const AlphaDropoutFuncOptions& options = {}) {
|
| 186 |
+
return detail::alpha_dropout(
|
| 187 |
+
std::move(input), options.p(), options.training(), options.inplace());
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
// ============================================================================
|
| 191 |
+
|
| 192 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 193 |
+
namespace detail {
|
| 194 |
+
|
| 195 |
+
inline Tensor feature_alpha_dropout(
|
| 196 |
+
Tensor input,
|
| 197 |
+
double p,
|
| 198 |
+
bool training,
|
| 199 |
+
bool inplace) {
|
| 200 |
+
if (p < 0. || p > 1.) {
|
| 201 |
+
TORCH_CHECK(
|
| 202 |
+
false, "dropout probability has to be between 0 and 1, but got ", p);
|
| 203 |
+
}
|
| 204 |
+
return inplace ? torch::feature_alpha_dropout_(input, p, training)
|
| 205 |
+
: torch::feature_alpha_dropout(input, p, training);
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
} // namespace detail
|
| 209 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 210 |
+
|
| 211 |
+
/// See
|
| 212 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.feature_alpha_dropout
|
| 213 |
+
/// about the exact behavior of this functional.
|
| 214 |
+
///
|
| 215 |
+
/// See the documentation for
|
| 216 |
+
/// `torch::nn::functional::FeatureAlphaDropoutFuncOptions` class to learn what
|
| 217 |
+
/// optional arguments are supported for this functional.
|
| 218 |
+
///
|
| 219 |
+
/// Example:
|
| 220 |
+
/// ```
|
| 221 |
+
/// namespace F = torch::nn::functional;
|
| 222 |
+
/// F::feature_alpha_dropout(input,
|
| 223 |
+
/// F::FeatureAlphaDropoutFuncOptions().p(0.5).training(false));
|
| 224 |
+
/// ```
|
| 225 |
+
inline Tensor feature_alpha_dropout(
|
| 226 |
+
Tensor input,
|
| 227 |
+
const FeatureAlphaDropoutFuncOptions& options = {}) {
|
| 228 |
+
return detail::feature_alpha_dropout(
|
| 229 |
+
std::move(input), options.p(), options.training(), options.inplace());
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
} // namespace functional
|
| 233 |
+
} // namespace nn
|
| 234 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/embedding.h
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/options/embedding.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace nn {
|
| 7 |
+
namespace functional {
|
| 8 |
+
|
| 9 |
+
inline Tensor one_hot(const Tensor& tensor, int64_t num_classes = -1) {
|
| 10 |
+
return torch::one_hot(tensor, num_classes);
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 14 |
+
namespace detail {
|
| 15 |
+
inline void _no_grad_embedding_renorm_(
|
| 16 |
+
Tensor weight,
|
| 17 |
+
const Tensor& input,
|
| 18 |
+
float max_norm,
|
| 19 |
+
float norm_type) {
|
| 20 |
+
torch::NoGradGuard no_grad;
|
| 21 |
+
torch::embedding_renorm_(weight, input, max_norm, norm_type);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
inline Tensor embedding(
|
| 25 |
+
const Tensor& input,
|
| 26 |
+
const Tensor& weight,
|
| 27 |
+
c10::optional<int64_t> padding_idx,
|
| 28 |
+
c10::optional<double> max_norm,
|
| 29 |
+
double norm_type,
|
| 30 |
+
bool scale_grad_by_freq,
|
| 31 |
+
bool sparse) {
|
| 32 |
+
auto input_ = input;
|
| 33 |
+
|
| 34 |
+
if (padding_idx != c10::nullopt) {
|
| 35 |
+
if (*padding_idx > 0) {
|
| 36 |
+
TORCH_CHECK(
|
| 37 |
+
*padding_idx < weight.size(0),
|
| 38 |
+
"Padding_idx must be within num_embeddings");
|
| 39 |
+
} else if (*padding_idx < 0) {
|
| 40 |
+
TORCH_CHECK(
|
| 41 |
+
*padding_idx >= -weight.size(0),
|
| 42 |
+
"Padding_idx must be within num_embedding");
|
| 43 |
+
padding_idx = weight.size(0) + *padding_idx;
|
| 44 |
+
}
|
| 45 |
+
} else {
|
| 46 |
+
padding_idx = -1;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
if (max_norm != c10::nullopt) {
|
| 50 |
+
input_ = input_.contiguous();
|
| 51 |
+
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
|
| 52 |
+
_no_grad_embedding_renorm_(weight, input_, *max_norm, norm_type);
|
| 53 |
+
}
|
| 54 |
+
return torch::embedding(
|
| 55 |
+
weight, input_, *padding_idx, scale_grad_by_freq, sparse);
|
| 56 |
+
}
|
| 57 |
+
} // namespace detail
|
| 58 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 59 |
+
|
| 60 |
+
/// See
|
| 61 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.embedding
|
| 62 |
+
/// about the exact behavior of this functional.
|
| 63 |
+
///
|
| 64 |
+
/// See the documentation for `torch::nn::functional::EmbeddingFuncOptions`
|
| 65 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 66 |
+
///
|
| 67 |
+
/// Example:
|
| 68 |
+
/// ```
|
| 69 |
+
/// namespace F = torch::nn::functional;
|
| 70 |
+
/// F::embedding(input, weight,
|
| 71 |
+
/// F::EmbeddingFuncOptions().norm_type(2.5).scale_grad_by_freq(true).sparse(true));
|
| 72 |
+
/// ```
|
| 73 |
+
inline Tensor embedding(
|
| 74 |
+
const Tensor& input,
|
| 75 |
+
const Tensor& weight,
|
| 76 |
+
const EmbeddingFuncOptions& options = {}) {
|
| 77 |
+
return detail::embedding(
|
| 78 |
+
input,
|
| 79 |
+
weight,
|
| 80 |
+
options.padding_idx(),
|
| 81 |
+
options.max_norm(),
|
| 82 |
+
options.norm_type(),
|
| 83 |
+
options.scale_grad_by_freq(),
|
| 84 |
+
options.sparse());
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 88 |
+
namespace detail {
|
| 89 |
+
inline Tensor embedding_bag(
|
| 90 |
+
const Tensor& input,
|
| 91 |
+
const Tensor& weight,
|
| 92 |
+
const Tensor& offsets,
|
| 93 |
+
c10::optional<double> max_norm,
|
| 94 |
+
double norm_type,
|
| 95 |
+
bool scale_grad_by_freq,
|
| 96 |
+
EmbeddingBagMode mode,
|
| 97 |
+
bool sparse,
|
| 98 |
+
const Tensor& per_sample_weights,
|
| 99 |
+
bool include_last_offset,
|
| 100 |
+
c10::optional<int64_t> padding_idx) {
|
| 101 |
+
auto input_ = input;
|
| 102 |
+
auto offsets_ = offsets;
|
| 103 |
+
auto per_sample_weights_ = per_sample_weights;
|
| 104 |
+
TORCH_CHECK(
|
| 105 |
+
!per_sample_weights_.defined() ||
|
| 106 |
+
input_.sizes() == per_sample_weights_.sizes(),
|
| 107 |
+
"embedding_bag: If per_sample_weights (",
|
| 108 |
+
per_sample_weights_.sizes(),
|
| 109 |
+
") is not null, then it must have the same shape as the input (",
|
| 110 |
+
input_.sizes(),
|
| 111 |
+
")");
|
| 112 |
+
if (input_.dim() == 2) {
|
| 113 |
+
TORCH_CHECK(
|
| 114 |
+
!offsets_.defined(),
|
| 115 |
+
"If input is 2D, then offsets has to be null, as input is treated is a mini-batch of fixed length sequences. However, found offsets of type Tensor");
|
| 116 |
+
offsets_ = torch::arange(
|
| 117 |
+
0,
|
| 118 |
+
input_.numel(),
|
| 119 |
+
input_.size(1),
|
| 120 |
+
torch::TensorOptions().dtype(torch::kLong).device(input_.device()));
|
| 121 |
+
input_ = input_.reshape(-1);
|
| 122 |
+
if (per_sample_weights_.defined()) {
|
| 123 |
+
per_sample_weights_ = per_sample_weights_.reshape(-1);
|
| 124 |
+
}
|
| 125 |
+
} else if (input_.dim() == 1) {
|
| 126 |
+
TORCH_CHECK(
|
| 127 |
+
offsets_.defined(), "offsets has to be a 1D Tensor but got null");
|
| 128 |
+
TORCH_CHECK(offsets_.dim() == 1, "offsets has to be a 1D Tensor");
|
| 129 |
+
} else {
|
| 130 |
+
TORCH_CHECK(
|
| 131 |
+
false,
|
| 132 |
+
"input has to be 1D or 2D Tensor, but got Tensor of dimension ",
|
| 133 |
+
input_.dim());
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
| 137 |
+
int mode_enum;
|
| 138 |
+
if (std::holds_alternative<enumtype::kSum>(mode)) {
|
| 139 |
+
mode_enum = 0;
|
| 140 |
+
} else if (std::holds_alternative<enumtype::kMean>(mode)) {
|
| 141 |
+
mode_enum = 1;
|
| 142 |
+
} else if (std::holds_alternative<enumtype::kMax>(mode)) {
|
| 143 |
+
mode_enum = 2;
|
| 144 |
+
TORCH_CHECK(
|
| 145 |
+
!scale_grad_by_freq,
|
| 146 |
+
"max mode does not support scaling the gradient by the frequency");
|
| 147 |
+
TORCH_CHECK(!sparse, "max mode does not support sparse weights");
|
| 148 |
+
} else {
|
| 149 |
+
TORCH_CHECK(false, "mode has to be one of sum, mean or max");
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
if (max_norm != c10::nullopt) {
|
| 153 |
+
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
|
| 154 |
+
_no_grad_embedding_renorm_(weight, input_, *max_norm, norm_type);
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
TORCH_CHECK(
|
| 158 |
+
!per_sample_weights_.defined() || std::get_if<enumtype::kSum>(&mode),
|
| 159 |
+
"embedding_bag: per_sample_weights was not null. ",
|
| 160 |
+
"per_sample_weights is only supported for mode='kSum' (got mode='",
|
| 161 |
+
torch::enumtype::get_enum_name(mode),
|
| 162 |
+
"').Please open a feature request on GitHub.");
|
| 163 |
+
|
| 164 |
+
return std::get<0>(torch::embedding_bag(
|
| 165 |
+
weight,
|
| 166 |
+
input_,
|
| 167 |
+
offsets_,
|
| 168 |
+
scale_grad_by_freq,
|
| 169 |
+
mode_enum,
|
| 170 |
+
sparse,
|
| 171 |
+
per_sample_weights_,
|
| 172 |
+
include_last_offset,
|
| 173 |
+
padding_idx));
|
| 174 |
+
}
|
| 175 |
+
} // namespace detail
|
| 176 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 177 |
+
|
| 178 |
+
/// See
|
| 179 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.embedding_bag
|
| 180 |
+
/// about the exact behavior of this functional.
|
| 181 |
+
///
|
| 182 |
+
/// See the documentation for `torch::nn::functional::EmbeddingBagFuncOptions`
|
| 183 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 184 |
+
///
|
| 185 |
+
/// Example:
|
| 186 |
+
/// ```
|
| 187 |
+
/// namespace F = torch::nn::functional;
|
| 188 |
+
/// F::embedding_bag(input, weight,
|
| 189 |
+
/// F::EmbeddingBagFuncOptions().mode(torch::kSum).offsets(offsets));
|
| 190 |
+
/// ```
|
| 191 |
+
inline Tensor embedding_bag(
|
| 192 |
+
const Tensor& input,
|
| 193 |
+
const Tensor& weight,
|
| 194 |
+
const EmbeddingBagFuncOptions& options = {}) {
|
| 195 |
+
return detail::embedding_bag(
|
| 196 |
+
input,
|
| 197 |
+
weight,
|
| 198 |
+
options.offsets(),
|
| 199 |
+
options.max_norm(),
|
| 200 |
+
options.norm_type(),
|
| 201 |
+
options.scale_grad_by_freq(),
|
| 202 |
+
options.mode(),
|
| 203 |
+
options.sparse(),
|
| 204 |
+
options.per_sample_weights(),
|
| 205 |
+
options.include_last_offset(),
|
| 206 |
+
options.padding_idx());
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
} // namespace functional
|
| 210 |
+
} // namespace nn
|
| 211 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/fold.h
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/options/fold.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace nn {
|
| 7 |
+
namespace functional {
|
| 8 |
+
|
| 9 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 10 |
+
namespace detail {
|
| 11 |
+
inline Tensor fold(
|
| 12 |
+
const Tensor& input,
|
| 13 |
+
ExpandingArray<2> output_size,
|
| 14 |
+
ExpandingArray<2> kernel_size,
|
| 15 |
+
ExpandingArray<2> dilation,
|
| 16 |
+
ExpandingArray<2> padding,
|
| 17 |
+
ExpandingArray<2> stride) {
|
| 18 |
+
if (input.dim() == 3 || input.dim() == 2) {
|
| 19 |
+
return torch::col2im(
|
| 20 |
+
input, output_size, kernel_size, dilation, padding, stride);
|
| 21 |
+
} else {
|
| 22 |
+
TORCH_CHECK(
|
| 23 |
+
false,
|
| 24 |
+
"Input Error: Only unbatched (2D) or batched (3D) input Tensors are supported "
|
| 25 |
+
"(got ",
|
| 26 |
+
input.dim(),
|
| 27 |
+
"D)");
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
} // namespace detail
|
| 31 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 32 |
+
|
| 33 |
+
/// See
|
| 34 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.fold
|
| 35 |
+
/// about the exact behavior of this functional.
|
| 36 |
+
///
|
| 37 |
+
/// See the documentation for `torch::nn::functional::FoldFuncOptions` class to
|
| 38 |
+
/// learn what optional arguments are supported for this functional.
|
| 39 |
+
///
|
| 40 |
+
/// Example:
|
| 41 |
+
/// ```
|
| 42 |
+
/// namespace F = torch::nn::functional;
|
| 43 |
+
/// F::fold(input, F::FoldFuncOptions({3, 2}, {2, 2}));
|
| 44 |
+
/// ```
|
| 45 |
+
inline Tensor fold(const Tensor& input, const FoldFuncOptions& options) {
|
| 46 |
+
return detail::fold(
|
| 47 |
+
input,
|
| 48 |
+
options.output_size(),
|
| 49 |
+
options.kernel_size(),
|
| 50 |
+
options.dilation(),
|
| 51 |
+
options.padding(),
|
| 52 |
+
options.stride());
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// ============================================================================
|
| 56 |
+
|
| 57 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 58 |
+
namespace detail {
|
| 59 |
+
inline Tensor unfold(
|
| 60 |
+
const Tensor& input,
|
| 61 |
+
ExpandingArray<2> kernel_size,
|
| 62 |
+
ExpandingArray<2> dilation,
|
| 63 |
+
ExpandingArray<2> padding,
|
| 64 |
+
ExpandingArray<2> stride) {
|
| 65 |
+
if (input.dim() == 4) {
|
| 66 |
+
return torch::im2col(input, kernel_size, dilation, padding, stride);
|
| 67 |
+
} else {
|
| 68 |
+
TORCH_CHECK(
|
| 69 |
+
false,
|
| 70 |
+
"Input Error: Only 4D input Tensors are supported "
|
| 71 |
+
"(got ",
|
| 72 |
+
input.dim(),
|
| 73 |
+
"D)");
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
} // namespace detail
|
| 77 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 78 |
+
|
| 79 |
+
/// See
|
| 80 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.unfold
|
| 81 |
+
/// about the exact behavior of this functional.
|
| 82 |
+
///
|
| 83 |
+
/// See the documentation for `torch::nn::functional::UnfoldFuncOptions` class
|
| 84 |
+
/// to learn what optional arguments are supported for this functional.
|
| 85 |
+
///
|
| 86 |
+
/// Example:
|
| 87 |
+
/// ```
|
| 88 |
+
/// namespace F = torch::nn::functional;
|
| 89 |
+
/// F::unfold(input, F::UnfoldFuncOptions({2, 2}).padding(1).stride(2));
|
| 90 |
+
/// ```
|
| 91 |
+
inline Tensor unfold(const Tensor& input, const UnfoldFuncOptions& options) {
|
| 92 |
+
return detail::unfold(
|
| 93 |
+
input,
|
| 94 |
+
options.kernel_size(),
|
| 95 |
+
options.dilation(),
|
| 96 |
+
options.padding(),
|
| 97 |
+
options.stride());
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
} // namespace functional
|
| 101 |
+
} // namespace nn
|
| 102 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/instancenorm.h
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/options/instancenorm.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace nn {
|
| 7 |
+
namespace functional {
|
| 8 |
+
|
| 9 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 10 |
+
namespace detail {
|
| 11 |
+
inline Tensor instance_norm(
|
| 12 |
+
const Tensor& input,
|
| 13 |
+
const Tensor& running_mean,
|
| 14 |
+
const Tensor& running_var,
|
| 15 |
+
const Tensor& weight,
|
| 16 |
+
const Tensor& bias,
|
| 17 |
+
bool use_input_stats,
|
| 18 |
+
double momentum,
|
| 19 |
+
double eps) {
|
| 20 |
+
return torch::instance_norm(
|
| 21 |
+
input,
|
| 22 |
+
weight,
|
| 23 |
+
bias,
|
| 24 |
+
running_mean,
|
| 25 |
+
running_var,
|
| 26 |
+
use_input_stats,
|
| 27 |
+
momentum,
|
| 28 |
+
eps,
|
| 29 |
+
at::globalContext().userEnabledCuDNN());
|
| 30 |
+
}
|
| 31 |
+
} // namespace detail
|
| 32 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 33 |
+
|
| 34 |
+
/// See
|
| 35 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.instance_norm
|
| 36 |
+
/// about the exact behavior of this functional.
|
| 37 |
+
///
|
| 38 |
+
/// See the documentation for `torch::nn::functional::InstanceNormFuncOptions`
|
| 39 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 40 |
+
///
|
| 41 |
+
/// Example:
|
| 42 |
+
/// ```
|
| 43 |
+
/// namespace F = torch::nn::functional;
|
| 44 |
+
/// F::instance_norm(input,
|
| 45 |
+
/// F::InstanceNormFuncOptions().running_mean(mean).running_var(variance).weight(weight).bias(bias).momentum(0.1).eps(1e-5));
|
| 46 |
+
/// ```
|
| 47 |
+
inline Tensor instance_norm(
|
| 48 |
+
const Tensor& input,
|
| 49 |
+
const InstanceNormFuncOptions& options = {}) {
|
| 50 |
+
return detail::instance_norm(
|
| 51 |
+
input,
|
| 52 |
+
options.running_mean(),
|
| 53 |
+
options.running_var(),
|
| 54 |
+
options.weight(),
|
| 55 |
+
options.bias(),
|
| 56 |
+
options.use_input_stats(),
|
| 57 |
+
options.momentum(),
|
| 58 |
+
options.eps());
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
} // namespace functional
|
| 62 |
+
} // namespace nn
|
| 63 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/linear.h
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/types.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace nn {
|
| 7 |
+
namespace functional {
|
| 8 |
+
|
| 9 |
+
inline Tensor bilinear(
|
| 10 |
+
const Tensor& input1,
|
| 11 |
+
const Tensor& input2,
|
| 12 |
+
const Tensor& weight,
|
| 13 |
+
const Tensor& bias = Tensor()) {
|
| 14 |
+
return torch::bilinear(input1, input2, weight, bias);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
// ============================================================================
|
| 18 |
+
|
| 19 |
+
inline Tensor linear(
|
| 20 |
+
const Tensor& input,
|
| 21 |
+
const Tensor& weight,
|
| 22 |
+
const Tensor& bias = {}) {
|
| 23 |
+
if (input.dim() == 2 && bias.defined()) {
|
| 24 |
+
// fused op is marginally faster
|
| 25 |
+
return torch::addmm(bias, input, weight.t());
|
| 26 |
+
} else {
|
| 27 |
+
auto output = input.matmul(weight.t());
|
| 28 |
+
if (bias.defined()) {
|
| 29 |
+
output += bias;
|
| 30 |
+
}
|
| 31 |
+
return output;
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
} // namespace functional
|
| 36 |
+
} // namespace nn
|
| 37 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/loss.h
ADDED
|
@@ -0,0 +1,1044 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/ExpandUtils.h>
|
| 4 |
+
#include <torch/nn/functional/activation.h>
|
| 5 |
+
#include <torch/nn/options/loss.h>
|
| 6 |
+
|
| 7 |
+
namespace torch {
|
| 8 |
+
namespace nn {
|
| 9 |
+
namespace functional {
|
| 10 |
+
|
| 11 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 12 |
+
namespace detail {
|
| 13 |
+
inline Tensor l1_loss(
|
| 14 |
+
const Tensor& input,
|
| 15 |
+
const Tensor& target,
|
| 16 |
+
L1LossFuncOptions::reduction_t reduction) {
|
| 17 |
+
return torch::l1_loss(input, target, enumtype::reduction_get_enum(reduction));
|
| 18 |
+
}
|
| 19 |
+
} // namespace detail
|
| 20 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 21 |
+
|
| 22 |
+
/// See
|
| 23 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.l1_loss
|
| 24 |
+
/// about the exact behavior of this functional.
|
| 25 |
+
///
|
| 26 |
+
/// See the documentation for `torch::nn::functional::L1LossFuncOptions` class
|
| 27 |
+
/// to learn what optional arguments are supported for this functional.
|
| 28 |
+
///
|
| 29 |
+
/// Example:
|
| 30 |
+
/// ```
|
| 31 |
+
/// namespace F = torch::nn::functional;
|
| 32 |
+
/// F::l1_loss(input, target, F::L1LossFuncOptions(torch::kNone));
|
| 33 |
+
/// ```
|
| 34 |
+
inline Tensor l1_loss(
|
| 35 |
+
const Tensor& input,
|
| 36 |
+
const Tensor& target,
|
| 37 |
+
const L1LossFuncOptions& options = {}) {
|
| 38 |
+
return detail::l1_loss(input, target, options.reduction());
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// ============================================================================
|
| 42 |
+
|
| 43 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 44 |
+
namespace detail {
|
| 45 |
+
inline Tensor kl_div(
|
| 46 |
+
const Tensor& input,
|
| 47 |
+
const Tensor& target,
|
| 48 |
+
KLDivFuncOptions::reduction_t reduction,
|
| 49 |
+
bool log_target = false) {
|
| 50 |
+
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
| 51 |
+
torch::Reduction::Reduction reduction_enum;
|
| 52 |
+
|
| 53 |
+
if (std::holds_alternative<enumtype::kMean>(reduction)) {
|
| 54 |
+
TORCH_WARN(
|
| 55 |
+
"reduction: 'mean' divides the total loss by both the batch size and the support size."
|
| 56 |
+
"'batchmean' divides only by the batch size, and aligns with the KL div math definition."
|
| 57 |
+
"'mean' will be changed to behave the same as 'batchmean' in the next major release.");
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// special case for batchmean
|
| 61 |
+
if (std::holds_alternative<enumtype::kBatchMean>(reduction)) {
|
| 62 |
+
reduction_enum = torch::Reduction::Sum;
|
| 63 |
+
} else {
|
| 64 |
+
reduction_enum = enumtype::reduction_get_enum(reduction);
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
auto reduced = torch::kl_div(input, target, reduction_enum, log_target);
|
| 68 |
+
|
| 69 |
+
if (std::holds_alternative<enumtype::kBatchMean>(reduction) &&
|
| 70 |
+
input.dim() != 0) {
|
| 71 |
+
reduced = reduced / input.sizes()[0];
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
return reduced;
|
| 75 |
+
}
|
| 76 |
+
} // namespace detail
|
| 77 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 78 |
+
|
| 79 |
+
/// See
|
| 80 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.kl_div
|
| 81 |
+
/// about the exact behavior of this functional.
|
| 82 |
+
///
|
| 83 |
+
/// See the documentation for `torch::nn::functional::KLDivFuncOptions` class to
|
| 84 |
+
/// learn what optional arguments are supported for this functional.
|
| 85 |
+
///
|
| 86 |
+
/// Example:
|
| 87 |
+
/// ```
|
| 88 |
+
/// namespace F = torch::nn::functional;
|
| 89 |
+
/// F::kl_div(input, target,
|
| 90 |
+
/// F::KLDivFuncOptions.reduction(torch::kNone).log_target(false));
|
| 91 |
+
/// ```
|
| 92 |
+
inline Tensor kl_div(
|
| 93 |
+
const Tensor& input,
|
| 94 |
+
const Tensor& target,
|
| 95 |
+
const KLDivFuncOptions& options = {}) {
|
| 96 |
+
return detail::kl_div(
|
| 97 |
+
input, target, options.reduction(), options.log_target());
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// ============================================================================
|
| 101 |
+
|
| 102 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 103 |
+
namespace detail {
|
| 104 |
+
inline Tensor mse_loss(
|
| 105 |
+
const Tensor& input,
|
| 106 |
+
const Tensor& target,
|
| 107 |
+
MSELossFuncOptions::reduction_t reduction) {
|
| 108 |
+
if (!(target.sizes() == input.sizes())) {
|
| 109 |
+
TORCH_WARN(
|
| 110 |
+
"Using a target size (",
|
| 111 |
+
target.sizes(),
|
| 112 |
+
") that is different to the input size (",
|
| 113 |
+
input.sizes(),
|
| 114 |
+
"). ",
|
| 115 |
+
"This will likely lead to incorrect results due to broadcasting. ",
|
| 116 |
+
"Please ensure they have the same size.");
|
| 117 |
+
}
|
| 118 |
+
std::vector<torch::Tensor> broadcast_tensors =
|
| 119 |
+
torch::broadcast_tensors({input, target});
|
| 120 |
+
auto expanded_input = broadcast_tensors[0];
|
| 121 |
+
auto expanded_target = broadcast_tensors[1];
|
| 122 |
+
return torch::mse_loss(
|
| 123 |
+
expanded_input, expanded_target, enumtype::reduction_get_enum(reduction));
|
| 124 |
+
}
|
| 125 |
+
} // namespace detail
|
| 126 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 127 |
+
|
| 128 |
+
/// See
|
| 129 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.mse_loss
|
| 130 |
+
/// about the exact behavior of this functional.
|
| 131 |
+
///
|
| 132 |
+
/// See the documentation for `torch::nn::functional::MSELossFuncOptions` class
|
| 133 |
+
/// to learn what optional arguments are supported for this functional.
|
| 134 |
+
///
|
| 135 |
+
/// Example:
|
| 136 |
+
/// ```
|
| 137 |
+
/// namespace F = torch::nn::functional;
|
| 138 |
+
/// F::mse_loss(input, target, F::MSELossFuncOptions(torch::kNone));
|
| 139 |
+
/// ```
|
| 140 |
+
inline Tensor mse_loss(
|
| 141 |
+
const Tensor& input,
|
| 142 |
+
const Tensor& target,
|
| 143 |
+
const MSELossFuncOptions& options = {}) {
|
| 144 |
+
return detail::mse_loss(input, target, options.reduction());
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
// ============================================================================
|
| 148 |
+
|
| 149 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 150 |
+
namespace detail {
|
| 151 |
+
inline Tensor binary_cross_entropy(
|
| 152 |
+
const Tensor& input,
|
| 153 |
+
const Tensor& target,
|
| 154 |
+
const Tensor& weight,
|
| 155 |
+
BinaryCrossEntropyFuncOptions::reduction_t reduction) {
|
| 156 |
+
auto reduction_enum = enumtype::reduction_get_enum(reduction);
|
| 157 |
+
|
| 158 |
+
if (target.sizes() != input.sizes()) {
|
| 159 |
+
TORCH_CHECK(
|
| 160 |
+
false,
|
| 161 |
+
"Using a target size (",
|
| 162 |
+
target.sizes(),
|
| 163 |
+
") ",
|
| 164 |
+
"that is different to the input size (",
|
| 165 |
+
input.sizes(),
|
| 166 |
+
") is deprecated. ",
|
| 167 |
+
"Please ensure they have the same size.");
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
auto weight_ = weight;
|
| 171 |
+
if (weight_.defined()) {
|
| 172 |
+
auto new_size = at::infer_size(target.sizes(), weight_.sizes());
|
| 173 |
+
weight_ = weight_.expand(new_size);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
return torch::binary_cross_entropy(input, target, weight_, reduction_enum);
|
| 177 |
+
}
|
| 178 |
+
} // namespace detail
|
| 179 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 180 |
+
|
| 181 |
+
/// See
|
| 182 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.binary_cross_entropy
|
| 183 |
+
/// about the exact behavior of this functional.
|
| 184 |
+
///
|
| 185 |
+
/// See the documentation for
|
| 186 |
+
/// `torch::nn::functional::BinaryCrossEntropyFuncOptions` class to learn what
|
| 187 |
+
/// optional arguments are supported for this functional.
|
| 188 |
+
///
|
| 189 |
+
/// Example:
|
| 190 |
+
/// ```
|
| 191 |
+
/// namespace F = torch::nn::functional;
|
| 192 |
+
/// F::binary_cross_entropy(input, target,
|
| 193 |
+
/// F::BinaryCrossEntropyFuncOptions().weight(weight));
|
| 194 |
+
/// ```
|
| 195 |
+
inline Tensor binary_cross_entropy(
|
| 196 |
+
const Tensor& input,
|
| 197 |
+
const Tensor& target,
|
| 198 |
+
const BinaryCrossEntropyFuncOptions& options = {}) {
|
| 199 |
+
return detail::binary_cross_entropy(
|
| 200 |
+
input, target, options.weight(), options.reduction());
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
// ============================================================================
|
| 204 |
+
|
| 205 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 206 |
+
namespace detail {
|
| 207 |
+
inline Tensor hinge_embedding_loss(
|
| 208 |
+
const Tensor& input,
|
| 209 |
+
const Tensor& target,
|
| 210 |
+
double margin,
|
| 211 |
+
HingeEmbeddingLossFuncOptions::reduction_t reduction) {
|
| 212 |
+
return torch::hinge_embedding_loss(
|
| 213 |
+
input, target, margin, enumtype::reduction_get_enum(reduction));
|
| 214 |
+
}
|
| 215 |
+
} // namespace detail
|
| 216 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 217 |
+
|
| 218 |
+
/// See
|
| 219 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.hinge_embedding_loss
|
| 220 |
+
/// about the exact behavior of this functional.
|
| 221 |
+
///
|
| 222 |
+
/// See the documentation for
|
| 223 |
+
/// `torch::nn::functional::HingeEmbeddingLossFuncOptions` class to learn what
|
| 224 |
+
/// optional arguments are supported for this functional.
|
| 225 |
+
///
|
| 226 |
+
/// Example:
|
| 227 |
+
/// ```
|
| 228 |
+
/// namespace F = torch::nn::functional;
|
| 229 |
+
/// F::hinge_embedding_loss(input, target,
|
| 230 |
+
/// F::HingeEmbeddingLossFuncOptions().margin(2));
|
| 231 |
+
/// ```
|
| 232 |
+
inline Tensor hinge_embedding_loss(
|
| 233 |
+
const Tensor& input,
|
| 234 |
+
const Tensor& target,
|
| 235 |
+
const HingeEmbeddingLossFuncOptions& options = {}) {
|
| 236 |
+
return detail::hinge_embedding_loss(
|
| 237 |
+
input, target, options.margin(), options.reduction());
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
// ============================================================================
|
| 241 |
+
|
| 242 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 243 |
+
namespace detail {
|
| 244 |
+
inline Tensor multi_margin_loss(
|
| 245 |
+
const Tensor& input,
|
| 246 |
+
const Tensor& target,
|
| 247 |
+
int64_t p,
|
| 248 |
+
double margin,
|
| 249 |
+
const Tensor& weight,
|
| 250 |
+
MultiMarginLossFuncOptions::reduction_t reduction) {
|
| 251 |
+
TORCH_CHECK(p == 1 || p == 2, "only p == 1 and p == 2 supported");
|
| 252 |
+
if (weight.defined()) {
|
| 253 |
+
TORCH_CHECK(weight.dim() == 1, "weight must be one-dimensional");
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
return torch::multi_margin_loss(
|
| 257 |
+
input,
|
| 258 |
+
target,
|
| 259 |
+
p,
|
| 260 |
+
margin,
|
| 261 |
+
weight,
|
| 262 |
+
enumtype::reduction_get_enum(reduction));
|
| 263 |
+
}
|
| 264 |
+
} // namespace detail
|
| 265 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 266 |
+
|
| 267 |
+
/// See
|
| 268 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.multi_margin_loss
|
| 269 |
+
/// about the exact behavior of this functional.
|
| 270 |
+
///
|
| 271 |
+
/// See the documentation for
|
| 272 |
+
/// `torch::nn::functional::MultiMarginLossFuncOptions` class to learn what
|
| 273 |
+
/// optional arguments are supported for this functional.
|
| 274 |
+
///
|
| 275 |
+
/// Example:
|
| 276 |
+
/// ```
|
| 277 |
+
/// namespace F = torch::nn::functional;
|
| 278 |
+
/// F::multi_margin_loss(input, target,
|
| 279 |
+
/// F::MultiMarginLossFuncOptions().margin(2).weight(weight));
|
| 280 |
+
/// ```
|
| 281 |
+
inline Tensor multi_margin_loss(
|
| 282 |
+
const Tensor& input,
|
| 283 |
+
const Tensor& target,
|
| 284 |
+
const MultiMarginLossFuncOptions& options = {}) {
|
| 285 |
+
return detail::multi_margin_loss(
|
| 286 |
+
input,
|
| 287 |
+
target,
|
| 288 |
+
options.p(),
|
| 289 |
+
options.margin(),
|
| 290 |
+
options.weight(),
|
| 291 |
+
options.reduction());
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
// ============================================================================
|
| 295 |
+
|
| 296 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 297 |
+
namespace detail {
|
| 298 |
+
inline Tensor cosine_embedding_loss(
|
| 299 |
+
const Tensor& input1,
|
| 300 |
+
const Tensor& input2,
|
| 301 |
+
const Tensor& target,
|
| 302 |
+
double margin,
|
| 303 |
+
CosineEmbeddingLossFuncOptions::reduction_t reduction) {
|
| 304 |
+
return torch::cosine_embedding_loss(
|
| 305 |
+
input1, input2, target, margin, enumtype::reduction_get_enum(reduction));
|
| 306 |
+
}
|
| 307 |
+
} // namespace detail
|
| 308 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 309 |
+
|
| 310 |
+
/// See
|
| 311 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.cosine_embedding_loss
|
| 312 |
+
/// about the exact behavior of this functional.
|
| 313 |
+
///
|
| 314 |
+
/// See the documentation for
|
| 315 |
+
/// `torch::nn::functional::CosineEmbeddingLossFuncOptions` class to learn what
|
| 316 |
+
/// optional arguments are supported for this functional.
|
| 317 |
+
///
|
| 318 |
+
/// Example:
|
| 319 |
+
/// ```
|
| 320 |
+
/// namespace F = torch::nn::functional;
|
| 321 |
+
/// F::cosine_embedding_loss(input1, input2, target,
|
| 322 |
+
/// F::CosineEmbeddingLossFuncOptions().margin(0.5));
|
| 323 |
+
/// ```
|
| 324 |
+
inline Tensor cosine_embedding_loss(
|
| 325 |
+
const Tensor& input1,
|
| 326 |
+
const Tensor& input2,
|
| 327 |
+
const Tensor& target,
|
| 328 |
+
const CosineEmbeddingLossFuncOptions& options = {}) {
|
| 329 |
+
return detail::cosine_embedding_loss(
|
| 330 |
+
input1, input2, target, options.margin(), options.reduction());
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// ============================================================================
|
| 334 |
+
|
| 335 |
+
inline Tensor _smooth_l1_loss(
|
| 336 |
+
const Tensor& input,
|
| 337 |
+
const Tensor& target,
|
| 338 |
+
double beta = 1.) {
|
| 339 |
+
auto t = torch::abs(input - target);
|
| 340 |
+
return torch::where(t < beta, 0.5 * torch::pow(t, 2) / beta, t - 0.5 * beta);
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 344 |
+
namespace detail {
|
| 345 |
+
inline Tensor smooth_l1_loss(
|
| 346 |
+
const Tensor& input,
|
| 347 |
+
const Tensor& target,
|
| 348 |
+
SmoothL1LossFuncOptions::reduction_t reduction,
|
| 349 |
+
c10::optional<double> beta_opt = c10::nullopt) {
|
| 350 |
+
if (target.sizes() != input.sizes()) {
|
| 351 |
+
TORCH_WARN(
|
| 352 |
+
"Using a target size (",
|
| 353 |
+
target.sizes(),
|
| 354 |
+
") that is different to the input size (",
|
| 355 |
+
input.sizes(),
|
| 356 |
+
"). ",
|
| 357 |
+
"This will likely lead to incorrect results due to broadcasting. ",
|
| 358 |
+
"Please ensure they have the same size.");
|
| 359 |
+
}
|
| 360 |
+
double beta = beta_opt.value_or(1.0);
|
| 361 |
+
|
| 362 |
+
std::vector<Tensor> expanded_tensors =
|
| 363 |
+
torch::broadcast_tensors({input, target});
|
| 364 |
+
return torch::smooth_l1_loss(
|
| 365 |
+
expanded_tensors[0],
|
| 366 |
+
expanded_tensors[1],
|
| 367 |
+
enumtype::reduction_get_enum(reduction),
|
| 368 |
+
beta);
|
| 369 |
+
}
|
| 370 |
+
} // namespace detail
|
| 371 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 372 |
+
|
| 373 |
+
/// See
|
| 374 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.smooth_l1_loss
|
| 375 |
+
/// about the exact behavior of this functional.
|
| 376 |
+
///
|
| 377 |
+
/// See the documentation for `torch::nn::functional::SmoothL1LossFuncOptions`
|
| 378 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 379 |
+
///
|
| 380 |
+
/// Example:
|
| 381 |
+
/// ```
|
| 382 |
+
/// namespace F = torch::nn::functional;
|
| 383 |
+
/// F::smooth_l1_loss(input, target, F::SmoothL1LossFuncOptions(torch::kNone));
|
| 384 |
+
/// ```
|
| 385 |
+
inline Tensor smooth_l1_loss(
|
| 386 |
+
const Tensor& input,
|
| 387 |
+
const Tensor& target,
|
| 388 |
+
const SmoothL1LossFuncOptions& options = {}) {
|
| 389 |
+
return detail::smooth_l1_loss(
|
| 390 |
+
input, target, options.reduction(), options.beta());
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
/// See
|
| 394 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.smooth_l1_loss
|
| 395 |
+
/// about the exact behavior of this functional.
|
| 396 |
+
///
|
| 397 |
+
/// Example:
|
| 398 |
+
/// ```
|
| 399 |
+
/// namespace F = torch::nn::functional;
|
| 400 |
+
/// F::smooth_l1_loss(input, target, /*options=*/torch::kNone, /*beta=*/0.5);
|
| 401 |
+
/// ```
|
| 402 |
+
inline Tensor smooth_l1_loss(
|
| 403 |
+
const Tensor& input,
|
| 404 |
+
const Tensor& target,
|
| 405 |
+
const SmoothL1LossFuncOptions& options,
|
| 406 |
+
double beta) {
|
| 407 |
+
TORCH_CHECK(
|
| 408 |
+
options.beta() == c10::nullopt,
|
| 409 |
+
"expected beta not to be provided in 'options', but got ",
|
| 410 |
+
options.beta().value());
|
| 411 |
+
return detail::smooth_l1_loss(input, target, options.reduction(), beta);
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
// ============================================================================
|
| 415 |
+
|
| 416 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 417 |
+
namespace detail {
|
| 418 |
+
inline Tensor huber_loss(
|
| 419 |
+
const Tensor& input,
|
| 420 |
+
const Tensor& target,
|
| 421 |
+
HuberLossFuncOptions::reduction_t reduction,
|
| 422 |
+
double delta = 1.) {
|
| 423 |
+
if (target.sizes() != input.sizes()) {
|
| 424 |
+
TORCH_WARN(
|
| 425 |
+
"Using a target size (",
|
| 426 |
+
target.sizes(),
|
| 427 |
+
") that is different to the input size (",
|
| 428 |
+
input.sizes(),
|
| 429 |
+
"). ",
|
| 430 |
+
"This will likely lead to incorrect results due to broadcasting. ",
|
| 431 |
+
"Please ensure they have the same size.");
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
std::vector<Tensor> expanded_tensors =
|
| 435 |
+
torch::broadcast_tensors({input, target});
|
| 436 |
+
return torch::huber_loss(
|
| 437 |
+
expanded_tensors[0],
|
| 438 |
+
expanded_tensors[1],
|
| 439 |
+
enumtype::reduction_get_enum(reduction),
|
| 440 |
+
delta);
|
| 441 |
+
}
|
| 442 |
+
} // namespace detail
|
| 443 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 444 |
+
|
| 445 |
+
/// See
|
| 446 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.huber_loss
|
| 447 |
+
/// about the exact behavior of this functional.
|
| 448 |
+
///
|
| 449 |
+
/// See the documentation for `torch::nn::functional::HuberLossFuncOptions`
|
| 450 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 451 |
+
///
|
| 452 |
+
/// Example:
|
| 453 |
+
/// ```
|
| 454 |
+
/// namespace F = torch::nn::functional;
|
| 455 |
+
/// F::huber_loss(input, target,
|
| 456 |
+
/// F::HuberLossFuncOptions().reduction(torch::kNone).delta(0.5));
|
| 457 |
+
/// ```
|
| 458 |
+
inline Tensor huber_loss(
|
| 459 |
+
const Tensor& input,
|
| 460 |
+
const Tensor& target,
|
| 461 |
+
const HuberLossFuncOptions& options = {}) {
|
| 462 |
+
return detail::huber_loss(
|
| 463 |
+
input, target, options.reduction(), options.delta());
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
// ============================================================================
|
| 467 |
+
|
| 468 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 469 |
+
namespace detail {
|
| 470 |
+
inline Tensor multilabel_margin_loss(
|
| 471 |
+
const Tensor& input,
|
| 472 |
+
const Tensor& target,
|
| 473 |
+
MultilabelMarginLossFuncOptions::reduction_t reduction) {
|
| 474 |
+
return torch::multilabel_margin_loss(
|
| 475 |
+
input, target, enumtype::reduction_get_enum(reduction));
|
| 476 |
+
}
|
| 477 |
+
} // namespace detail
|
| 478 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 479 |
+
|
| 480 |
+
/// See
|
| 481 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.multilabel_margin_loss
|
| 482 |
+
/// about the exact behavior of this functional.
|
| 483 |
+
///
|
| 484 |
+
/// See the documentation for
|
| 485 |
+
/// `torch::nn::functional::MultilabelMarginLossFuncOptions` class to learn what
|
| 486 |
+
/// optional arguments are supported for this functional.
|
| 487 |
+
///
|
| 488 |
+
/// Example:
|
| 489 |
+
/// ```
|
| 490 |
+
/// namespace F = torch::nn::functional;
|
| 491 |
+
/// F::multilabel_margin_loss(input, target,
|
| 492 |
+
/// F::MultilabelMarginLossFuncOptions(torch::kNone));
|
| 493 |
+
/// ```
|
| 494 |
+
inline Tensor multilabel_margin_loss(
|
| 495 |
+
const Tensor& input,
|
| 496 |
+
const Tensor& target,
|
| 497 |
+
const MultilabelMarginLossFuncOptions& options = {}) {
|
| 498 |
+
return detail::multilabel_margin_loss(input, target, options.reduction());
|
| 499 |
+
}
|
| 500 |
+
|
| 501 |
+
// ============================================================================
|
| 502 |
+
|
| 503 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 504 |
+
namespace detail {
|
| 505 |
+
inline Tensor soft_margin_loss(
|
| 506 |
+
const Tensor& input,
|
| 507 |
+
const Tensor& target,
|
| 508 |
+
SoftMarginLossFuncOptions::reduction_t reduction) {
|
| 509 |
+
return torch::soft_margin_loss(
|
| 510 |
+
input, target, enumtype::reduction_get_enum(reduction));
|
| 511 |
+
}
|
| 512 |
+
} // namespace detail
|
| 513 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 514 |
+
|
| 515 |
+
/// See
|
| 516 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.soft_margin_loss
|
| 517 |
+
/// about the exact behavior of this functional.
|
| 518 |
+
///
|
| 519 |
+
/// See the documentation for `torch::nn::functional::SoftMarginLossFuncOptions`
|
| 520 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 521 |
+
///
|
| 522 |
+
/// Example:
|
| 523 |
+
/// ```
|
| 524 |
+
/// namespace F = torch::nn::functional;
|
| 525 |
+
/// F::soft_margin_loss(input, target,
|
| 526 |
+
/// F::SoftMarginLossFuncOptions(torch::kNone));
|
| 527 |
+
/// ```
|
| 528 |
+
inline Tensor soft_margin_loss(
|
| 529 |
+
const Tensor& input,
|
| 530 |
+
const Tensor& target,
|
| 531 |
+
const SoftMarginLossFuncOptions& options = {}) {
|
| 532 |
+
return detail::soft_margin_loss(input, target, options.reduction());
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
// ============================================================================
|
| 536 |
+
|
| 537 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 538 |
+
namespace detail {
|
| 539 |
+
inline Tensor multilabel_soft_margin_loss(
|
| 540 |
+
const Tensor& input,
|
| 541 |
+
const Tensor& target,
|
| 542 |
+
const Tensor& weight,
|
| 543 |
+
MultilabelSoftMarginLossFuncOptions::reduction_t reduction) {
|
| 544 |
+
auto loss =
|
| 545 |
+
-(target * torch::log_sigmoid(input) +
|
| 546 |
+
(1 - target) * torch::log_sigmoid(-input));
|
| 547 |
+
if (weight.defined()) {
|
| 548 |
+
loss = loss * weight;
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
auto class_dim = input.dim() - 1;
|
| 552 |
+
auto C = input.size(class_dim);
|
| 553 |
+
loss = loss.sum(class_dim) / C; // only return N loss values
|
| 554 |
+
|
| 555 |
+
Tensor ret;
|
| 556 |
+
|
| 557 |
+
if (std::holds_alternative<enumtype::kNone>(reduction)) {
|
| 558 |
+
ret = loss;
|
| 559 |
+
} else if (std::holds_alternative<enumtype::kMean>(reduction)) {
|
| 560 |
+
ret = loss.mean();
|
| 561 |
+
} else if (std::holds_alternative<enumtype::kSum>(reduction)) {
|
| 562 |
+
ret = loss.sum();
|
| 563 |
+
} else {
|
| 564 |
+
ret = input;
|
| 565 |
+
TORCH_INTERNAL_ASSERT(
|
| 566 |
+
false, enumtype::get_enum_name(reduction), " is not valid");
|
| 567 |
+
}
|
| 568 |
+
return ret;
|
| 569 |
+
}
|
| 570 |
+
} // namespace detail
|
| 571 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 572 |
+
|
| 573 |
+
/// See
|
| 574 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.multilabel_soft_margin_loss
|
| 575 |
+
/// about the exact behavior of this functional.
|
| 576 |
+
///
|
| 577 |
+
/// See the documentation for
|
| 578 |
+
/// `torch::nn::functional::MultilabelSoftMarginLossFuncOptions` class to learn
|
| 579 |
+
/// what optional arguments are supported for this functional.
|
| 580 |
+
///
|
| 581 |
+
/// Example:
|
| 582 |
+
/// ```
|
| 583 |
+
/// namespace F = torch::nn::functional;
|
| 584 |
+
/// F::multilabel_soft_margin_loss(input, target,
|
| 585 |
+
/// F::MultilabelSoftMarginLossFuncOptions().reduction(torch::kNone).weight(weight));
|
| 586 |
+
/// ```
|
| 587 |
+
inline Tensor multilabel_soft_margin_loss(
|
| 588 |
+
const Tensor& input,
|
| 589 |
+
const Tensor& target,
|
| 590 |
+
const MultilabelSoftMarginLossFuncOptions& options = {}) {
|
| 591 |
+
return detail::multilabel_soft_margin_loss(
|
| 592 |
+
input, target, options.weight(), options.reduction());
|
| 593 |
+
}
|
| 594 |
+
|
| 595 |
+
// ============================================================================
|
| 596 |
+
|
| 597 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 598 |
+
namespace detail {
|
| 599 |
+
inline Tensor triplet_margin_loss(
|
| 600 |
+
const Tensor& anchor,
|
| 601 |
+
const Tensor& positive,
|
| 602 |
+
const Tensor& negative,
|
| 603 |
+
double margin,
|
| 604 |
+
double p,
|
| 605 |
+
double eps,
|
| 606 |
+
bool swap,
|
| 607 |
+
TripletMarginLossFuncOptions::reduction_t reduction) {
|
| 608 |
+
return torch::triplet_margin_loss(
|
| 609 |
+
anchor,
|
| 610 |
+
positive,
|
| 611 |
+
negative,
|
| 612 |
+
margin,
|
| 613 |
+
p,
|
| 614 |
+
eps,
|
| 615 |
+
swap,
|
| 616 |
+
enumtype::reduction_get_enum(reduction));
|
| 617 |
+
}
|
| 618 |
+
} // namespace detail
|
| 619 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 620 |
+
|
| 621 |
+
/// See
|
| 622 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.triplet_margin_loss
|
| 623 |
+
/// about the exact behavior of this functional.
|
| 624 |
+
///
|
| 625 |
+
/// See the documentation for
|
| 626 |
+
/// `torch::nn::functional::TripletMarginLossFuncOptions` class to learn what
|
| 627 |
+
/// optional arguments are supported for this functional.
|
| 628 |
+
///
|
| 629 |
+
/// Example:
|
| 630 |
+
/// ```
|
| 631 |
+
/// namespace F = torch::nn::functional;
|
| 632 |
+
/// F::triplet_margin_loss(anchor, positive, negative,
|
| 633 |
+
/// F::TripletMarginLossFuncOptions().margin(1.0));
|
| 634 |
+
/// ```
|
| 635 |
+
inline Tensor triplet_margin_loss(
|
| 636 |
+
const Tensor& anchor,
|
| 637 |
+
const Tensor& positive,
|
| 638 |
+
const Tensor& negative,
|
| 639 |
+
const TripletMarginLossFuncOptions& options = {}) {
|
| 640 |
+
return detail::triplet_margin_loss(
|
| 641 |
+
anchor,
|
| 642 |
+
positive,
|
| 643 |
+
negative,
|
| 644 |
+
options.margin(),
|
| 645 |
+
options.p(),
|
| 646 |
+
options.eps(),
|
| 647 |
+
options.swap(),
|
| 648 |
+
options.reduction());
|
| 649 |
+
}
|
| 650 |
+
|
| 651 |
+
// ============================================================================
|
| 652 |
+
|
| 653 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 654 |
+
namespace detail {
|
| 655 |
+
inline Tensor triplet_margin_with_distance_loss(
|
| 656 |
+
const Tensor& anchor,
|
| 657 |
+
const Tensor& positive,
|
| 658 |
+
const Tensor& negative,
|
| 659 |
+
c10::optional<TripletMarginWithDistanceLossFuncOptions::distance_function_t>
|
| 660 |
+
distance_function,
|
| 661 |
+
double margin,
|
| 662 |
+
bool swap,
|
| 663 |
+
TripletMarginWithDistanceLossFuncOptions::reduction_t reduction) {
|
| 664 |
+
Tensor dist_pos, dist_neg;
|
| 665 |
+
if (distance_function.has_value()) {
|
| 666 |
+
auto distance_function_impl = distance_function.value();
|
| 667 |
+
dist_pos = distance_function_impl(anchor, positive);
|
| 668 |
+
dist_neg = distance_function_impl(anchor, negative);
|
| 669 |
+
} else {
|
| 670 |
+
dist_pos = pairwise_distance(anchor, positive);
|
| 671 |
+
dist_neg = pairwise_distance(anchor, negative);
|
| 672 |
+
}
|
| 673 |
+
|
| 674 |
+
if (swap) {
|
| 675 |
+
Tensor dist_swap;
|
| 676 |
+
if (distance_function.has_value()) {
|
| 677 |
+
dist_swap = distance_function.value()(positive, negative);
|
| 678 |
+
} else {
|
| 679 |
+
dist_swap = pairwise_distance(positive, negative);
|
| 680 |
+
}
|
| 681 |
+
dist_neg = torch::min(dist_neg, dist_swap);
|
| 682 |
+
}
|
| 683 |
+
|
| 684 |
+
auto loss = torch::clamp_min(dist_pos - dist_neg + margin, 0);
|
| 685 |
+
|
| 686 |
+
Tensor ret;
|
| 687 |
+
if (std::holds_alternative<enumtype::kNone>(reduction)) {
|
| 688 |
+
ret = loss;
|
| 689 |
+
} else if (std::holds_alternative<enumtype::kMean>(reduction)) {
|
| 690 |
+
ret = loss.mean();
|
| 691 |
+
} else if (std::holds_alternative<enumtype::kSum>(reduction)) {
|
| 692 |
+
ret = loss.sum();
|
| 693 |
+
} else {
|
| 694 |
+
ret = anchor;
|
| 695 |
+
TORCH_INTERNAL_ASSERT(
|
| 696 |
+
false, enumtype::get_enum_name(reduction), " is not valid");
|
| 697 |
+
}
|
| 698 |
+
return ret;
|
| 699 |
+
}
|
| 700 |
+
} // namespace detail
|
| 701 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 702 |
+
|
| 703 |
+
/// See
|
| 704 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.triplet_margin_with_distance_loss
|
| 705 |
+
/// about the exact behavior of this functional.
|
| 706 |
+
///
|
| 707 |
+
/// See the documentation for
|
| 708 |
+
/// `torch::nn::functional::TripletMarginWithDistanceLossFuncOptions` class to
|
| 709 |
+
/// learn what optional arguments are supported for this functional.
|
| 710 |
+
///
|
| 711 |
+
/// Example:
|
| 712 |
+
/// ```
|
| 713 |
+
/// namespace F = torch::nn::functional;
|
| 714 |
+
/// F::triplet_margin_with_distance_loss(anchor, positive, negative,
|
| 715 |
+
/// F::TripletMarginWithDistanceLossFuncOptions().margin(1.0));
|
| 716 |
+
/// ```
|
| 717 |
+
inline Tensor triplet_margin_with_distance_loss(
|
| 718 |
+
const Tensor& anchor,
|
| 719 |
+
const Tensor& positive,
|
| 720 |
+
const Tensor& negative,
|
| 721 |
+
const TripletMarginWithDistanceLossFuncOptions& options = {}) {
|
| 722 |
+
return detail::triplet_margin_with_distance_loss(
|
| 723 |
+
anchor,
|
| 724 |
+
positive,
|
| 725 |
+
negative,
|
| 726 |
+
options.distance_function(),
|
| 727 |
+
options.margin(),
|
| 728 |
+
options.swap(),
|
| 729 |
+
options.reduction());
|
| 730 |
+
}
|
| 731 |
+
|
| 732 |
+
// ============================================================================
|
| 733 |
+
|
| 734 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 735 |
+
namespace detail {
|
| 736 |
+
inline Tensor ctc_loss(
|
| 737 |
+
const Tensor& log_probs,
|
| 738 |
+
const Tensor& targets,
|
| 739 |
+
const Tensor& input_lengths,
|
| 740 |
+
const Tensor& target_lengths,
|
| 741 |
+
int64_t blank,
|
| 742 |
+
CTCLossFuncOptions::reduction_t reduction,
|
| 743 |
+
bool zero_infinity) {
|
| 744 |
+
return torch::ctc_loss(
|
| 745 |
+
log_probs,
|
| 746 |
+
targets,
|
| 747 |
+
input_lengths,
|
| 748 |
+
target_lengths,
|
| 749 |
+
blank,
|
| 750 |
+
enumtype::reduction_get_enum(reduction),
|
| 751 |
+
zero_infinity);
|
| 752 |
+
}
|
| 753 |
+
} // namespace detail
|
| 754 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 755 |
+
|
| 756 |
+
/// See
|
| 757 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.ctc_loss
|
| 758 |
+
/// about the exact behavior of this functional.
|
| 759 |
+
///
|
| 760 |
+
/// See the documentation for `torch::nn::functional::CTCLossFuncOptions` class
|
| 761 |
+
/// to learn what optional arguments are supported for this functional.
|
| 762 |
+
///
|
| 763 |
+
/// Example:
|
| 764 |
+
/// ```
|
| 765 |
+
/// namespace F = torch::nn::functional;
|
| 766 |
+
/// F::ctc_loss(log_probs, targets, input_lengths, target_lengths,
|
| 767 |
+
/// F::CTCLossFuncOptions().reduction(torch::kNone));
|
| 768 |
+
/// ```
|
| 769 |
+
inline Tensor ctc_loss(
|
| 770 |
+
const Tensor& log_probs,
|
| 771 |
+
const Tensor& targets,
|
| 772 |
+
const Tensor& input_lengths,
|
| 773 |
+
const Tensor& target_lengths,
|
| 774 |
+
const CTCLossFuncOptions& options = {}) {
|
| 775 |
+
return detail::ctc_loss(
|
| 776 |
+
log_probs,
|
| 777 |
+
targets,
|
| 778 |
+
input_lengths,
|
| 779 |
+
target_lengths,
|
| 780 |
+
options.blank(),
|
| 781 |
+
options.reduction(),
|
| 782 |
+
options.zero_infinity());
|
| 783 |
+
}
|
| 784 |
+
|
| 785 |
+
// ============================================================================
|
| 786 |
+
|
| 787 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 788 |
+
namespace detail {
|
| 789 |
+
inline Tensor poisson_nll_loss(
|
| 790 |
+
const Tensor& input,
|
| 791 |
+
const Tensor& target,
|
| 792 |
+
bool log_input,
|
| 793 |
+
bool full,
|
| 794 |
+
double eps,
|
| 795 |
+
PoissonNLLLossFuncOptions::reduction_t reduction) {
|
| 796 |
+
return torch::poisson_nll_loss(
|
| 797 |
+
input,
|
| 798 |
+
target,
|
| 799 |
+
log_input,
|
| 800 |
+
full,
|
| 801 |
+
eps,
|
| 802 |
+
enumtype::reduction_get_enum(reduction));
|
| 803 |
+
}
|
| 804 |
+
} // namespace detail
|
| 805 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 806 |
+
|
| 807 |
+
/// See
|
| 808 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.poisson_nll_loss
|
| 809 |
+
/// about the exact behavior of this functional.
|
| 810 |
+
///
|
| 811 |
+
/// See the documentation for `torch::nn::functional::PoissonNLLLossFuncOptions`
|
| 812 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 813 |
+
///
|
| 814 |
+
/// Example:
|
| 815 |
+
/// ```
|
| 816 |
+
/// namespace F = torch::nn::functional;
|
| 817 |
+
/// F::poisson_nll_loss(input, target,
|
| 818 |
+
/// F::PoissonNLLLossFuncOptions().reduction(torch::kNone));
|
| 819 |
+
/// ```
|
| 820 |
+
inline Tensor poisson_nll_loss(
|
| 821 |
+
const Tensor& input,
|
| 822 |
+
const Tensor& target,
|
| 823 |
+
const PoissonNLLLossFuncOptions& options = {}) {
|
| 824 |
+
return detail::poisson_nll_loss(
|
| 825 |
+
input,
|
| 826 |
+
target,
|
| 827 |
+
options.log_input(),
|
| 828 |
+
options.full(),
|
| 829 |
+
options.eps(),
|
| 830 |
+
options.reduction());
|
| 831 |
+
}
|
| 832 |
+
|
| 833 |
+
// ============================================================================
|
| 834 |
+
|
| 835 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 836 |
+
namespace detail {
|
| 837 |
+
inline Tensor margin_ranking_loss(
|
| 838 |
+
const Tensor& input1,
|
| 839 |
+
const Tensor& input2,
|
| 840 |
+
const Tensor& target,
|
| 841 |
+
double margin,
|
| 842 |
+
MarginRankingLossFuncOptions::reduction_t reduction) {
|
| 843 |
+
TORCH_CHECK(
|
| 844 |
+
input1.dim() == input2.dim() && input1.dim() == target.dim(),
|
| 845 |
+
"margin_ranking_loss : All input tensors should have same dimension but got sizes: "
|
| 846 |
+
"input1: ",
|
| 847 |
+
input1.sizes(),
|
| 848 |
+
", input2: ",
|
| 849 |
+
input2.sizes(),
|
| 850 |
+
", target: ",
|
| 851 |
+
target.sizes());
|
| 852 |
+
return torch::margin_ranking_loss(
|
| 853 |
+
input1, input2, target, margin, enumtype::reduction_get_enum(reduction));
|
| 854 |
+
}
|
| 855 |
+
} // namespace detail
|
| 856 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 857 |
+
|
| 858 |
+
/// See
|
| 859 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.margin_ranking_loss
|
| 860 |
+
/// about the exact behavior of this functional.
|
| 861 |
+
///
|
| 862 |
+
/// See the documentation for
|
| 863 |
+
/// `torch::nn::functional::MarginRankingLossFuncOptions` class to learn what
|
| 864 |
+
/// optional arguments are supported for this functional.
|
| 865 |
+
///
|
| 866 |
+
/// Example:
|
| 867 |
+
/// ```
|
| 868 |
+
/// namespace F = torch::nn::functional;
|
| 869 |
+
/// F::margin_ranking_loss(input1, input2, target,
|
| 870 |
+
/// F::MarginRankingLossFuncOptions().margin(0.5).reduction(torch::kSum));
|
| 871 |
+
/// ```
|
| 872 |
+
inline Tensor margin_ranking_loss(
|
| 873 |
+
const Tensor& input1,
|
| 874 |
+
const Tensor& input2,
|
| 875 |
+
const Tensor& target,
|
| 876 |
+
const MarginRankingLossFuncOptions& options = {}) {
|
| 877 |
+
return detail::margin_ranking_loss(
|
| 878 |
+
input1, input2, target, options.margin(), options.reduction());
|
| 879 |
+
}
|
| 880 |
+
|
| 881 |
+
// ============================================================================
|
| 882 |
+
|
| 883 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 884 |
+
namespace detail {
|
| 885 |
+
inline Tensor nll_loss(
|
| 886 |
+
const Tensor& input,
|
| 887 |
+
const Tensor& target,
|
| 888 |
+
const Tensor& weight,
|
| 889 |
+
int64_t ignore_index,
|
| 890 |
+
const NLLLossFuncOptions::reduction_t reduction) {
|
| 891 |
+
if (input.dim() < 2) {
|
| 892 |
+
TORCH_CHECK(false, "Expected 2 or more dimensions (got ", input.dim(), ")");
|
| 893 |
+
}
|
| 894 |
+
|
| 895 |
+
if (input.sizes()[0] != target.sizes()[0]) {
|
| 896 |
+
TORCH_CHECK(
|
| 897 |
+
false,
|
| 898 |
+
"Expected input batch_size (",
|
| 899 |
+
input.sizes()[0],
|
| 900 |
+
") to match target batch_size (",
|
| 901 |
+
target.sizes()[0],
|
| 902 |
+
").");
|
| 903 |
+
}
|
| 904 |
+
|
| 905 |
+
return torch::nll_loss_nd(
|
| 906 |
+
input,
|
| 907 |
+
target,
|
| 908 |
+
weight,
|
| 909 |
+
enumtype::reduction_get_enum(reduction),
|
| 910 |
+
ignore_index);
|
| 911 |
+
}
|
| 912 |
+
} // namespace detail
|
| 913 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 914 |
+
|
| 915 |
+
/// See
|
| 916 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.nll_loss
|
| 917 |
+
/// about the exact behavior of this functional.
|
| 918 |
+
///
|
| 919 |
+
/// See the documentation for `torch::nn::functional::NLLLossFuncOptions` class
|
| 920 |
+
/// to learn what optional arguments are supported for this functional.
|
| 921 |
+
///
|
| 922 |
+
/// Example:
|
| 923 |
+
/// ```
|
| 924 |
+
/// namespace F = torch::nn::functional;
|
| 925 |
+
/// F::nll_loss(input, target,
|
| 926 |
+
/// F::NLLLossFuncOptions().ignore_index(-100).reduction(torch::kMean));
|
| 927 |
+
/// ```
|
| 928 |
+
inline Tensor nll_loss(
|
| 929 |
+
const Tensor& input,
|
| 930 |
+
const Tensor& target,
|
| 931 |
+
const NLLLossFuncOptions& options = {}) {
|
| 932 |
+
return detail::nll_loss(
|
| 933 |
+
input,
|
| 934 |
+
target,
|
| 935 |
+
options.weight(),
|
| 936 |
+
options.ignore_index(),
|
| 937 |
+
options.reduction());
|
| 938 |
+
}
|
| 939 |
+
|
| 940 |
+
// ============================================================================
|
| 941 |
+
|
| 942 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 943 |
+
namespace detail {
|
| 944 |
+
inline Tensor cross_entropy(
|
| 945 |
+
const Tensor& input,
|
| 946 |
+
const Tensor& target,
|
| 947 |
+
const Tensor& weight,
|
| 948 |
+
int64_t ignore_index,
|
| 949 |
+
CrossEntropyFuncOptions::reduction_t reduction,
|
| 950 |
+
double label_smoothing) {
|
| 951 |
+
return torch::cross_entropy_loss(
|
| 952 |
+
input,
|
| 953 |
+
target,
|
| 954 |
+
weight,
|
| 955 |
+
enumtype::reduction_get_enum(reduction),
|
| 956 |
+
ignore_index,
|
| 957 |
+
label_smoothing);
|
| 958 |
+
}
|
| 959 |
+
} // namespace detail
|
| 960 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 961 |
+
|
| 962 |
+
/// See
|
| 963 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.cross_entropy
|
| 964 |
+
/// about the exact behavior of this functional.
|
| 965 |
+
///
|
| 966 |
+
/// See the documentation for `torch::nn::functional::CrossEntropyFuncOptions`
|
| 967 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 968 |
+
///
|
| 969 |
+
/// Example:
|
| 970 |
+
/// ```
|
| 971 |
+
/// namespace F = torch::nn::functional;
|
| 972 |
+
/// F::cross_entropy(input, target,
|
| 973 |
+
/// F::CrossEntropyFuncOptions().ignore_index(-100).reduction(torch::kMean));
|
| 974 |
+
/// ```
|
| 975 |
+
inline Tensor cross_entropy(
|
| 976 |
+
const Tensor& input,
|
| 977 |
+
const Tensor& target,
|
| 978 |
+
const CrossEntropyFuncOptions& options = {}) {
|
| 979 |
+
return detail::cross_entropy(
|
| 980 |
+
input,
|
| 981 |
+
target,
|
| 982 |
+
options.weight(),
|
| 983 |
+
options.ignore_index(),
|
| 984 |
+
options.reduction(),
|
| 985 |
+
options.label_smoothing());
|
| 986 |
+
}
|
| 987 |
+
|
| 988 |
+
// ============================================================================
|
| 989 |
+
|
| 990 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 991 |
+
namespace detail {
|
| 992 |
+
inline Tensor binary_cross_entropy_with_logits(
|
| 993 |
+
const Tensor& input,
|
| 994 |
+
const Tensor& target,
|
| 995 |
+
const Tensor& weight,
|
| 996 |
+
BinaryCrossEntropyWithLogitsFuncOptions::reduction_t reduction,
|
| 997 |
+
const Tensor& pos_weight) {
|
| 998 |
+
TORCH_CHECK(
|
| 999 |
+
target.sizes() == input.sizes(),
|
| 1000 |
+
"Target size (",
|
| 1001 |
+
target.sizes(),
|
| 1002 |
+
") must be the same as input size (",
|
| 1003 |
+
input.sizes(),
|
| 1004 |
+
")");
|
| 1005 |
+
|
| 1006 |
+
return torch::binary_cross_entropy_with_logits(
|
| 1007 |
+
input,
|
| 1008 |
+
target,
|
| 1009 |
+
weight,
|
| 1010 |
+
pos_weight,
|
| 1011 |
+
enumtype::reduction_get_enum(reduction));
|
| 1012 |
+
}
|
| 1013 |
+
} // namespace detail
|
| 1014 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 1015 |
+
|
| 1016 |
+
/// See
|
| 1017 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.binary_cross_entropy_with_logits
|
| 1018 |
+
/// about the exact behavior of this functional.
|
| 1019 |
+
///
|
| 1020 |
+
/// See the documentation for
|
| 1021 |
+
/// `torch::nn::functional::BinaryCrossEntropyWithLogitsFuncOptions` class to
|
| 1022 |
+
/// learn what optional arguments are supported for this functional.
|
| 1023 |
+
///
|
| 1024 |
+
/// Example:
|
| 1025 |
+
/// ```
|
| 1026 |
+
/// namespace F = torch::nn::functional;
|
| 1027 |
+
/// F::binary_cross_entropy_with_logits(input, target,
|
| 1028 |
+
/// F::BinaryCrossEntropyWithLogitsFuncOptions().pos_weight(pos_weight).reduction(torch::kSum));
|
| 1029 |
+
/// ```
|
| 1030 |
+
inline Tensor binary_cross_entropy_with_logits(
|
| 1031 |
+
const Tensor& input,
|
| 1032 |
+
const Tensor& target,
|
| 1033 |
+
const BinaryCrossEntropyWithLogitsFuncOptions& options = {}) {
|
| 1034 |
+
return detail::binary_cross_entropy_with_logits(
|
| 1035 |
+
input,
|
| 1036 |
+
target,
|
| 1037 |
+
options.weight(),
|
| 1038 |
+
options.reduction(),
|
| 1039 |
+
options.pos_weight());
|
| 1040 |
+
}
|
| 1041 |
+
|
| 1042 |
+
} // namespace functional
|
| 1043 |
+
} // namespace nn
|
| 1044 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/normalization.h
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/functional/padding.h>
|
| 4 |
+
#include <torch/nn/functional/pooling.h>
|
| 5 |
+
#include <torch/nn/options/normalization.h>
|
| 6 |
+
#include <torch/types.h>
|
| 7 |
+
|
| 8 |
+
namespace torch {
|
| 9 |
+
namespace nn {
|
| 10 |
+
namespace functional {
|
| 11 |
+
|
| 12 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 13 |
+
namespace detail {
|
| 14 |
+
inline Tensor normalize(
|
| 15 |
+
const Tensor& input,
|
| 16 |
+
double p,
|
| 17 |
+
int64_t dim,
|
| 18 |
+
double eps,
|
| 19 |
+
c10::optional<Tensor> out) {
|
| 20 |
+
if (out == c10::nullopt) {
|
| 21 |
+
auto denom = input.norm(p, dim, true).clamp_min(eps).expand_as(input);
|
| 22 |
+
return input / denom;
|
| 23 |
+
} else {
|
| 24 |
+
auto denom = input.norm(p, dim, true).clamp_min(eps).expand_as(input);
|
| 25 |
+
return torch::div_out(*out, input, denom);
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
} // namespace detail
|
| 29 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 30 |
+
|
| 31 |
+
/// See
|
| 32 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.normalize
|
| 33 |
+
/// about the exact behavior of this functional.
|
| 34 |
+
///
|
| 35 |
+
/// See the documentation for `torch::nn::functional::NormalizeFuncOptions`
|
| 36 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 37 |
+
///
|
| 38 |
+
/// Example:
|
| 39 |
+
/// ```
|
| 40 |
+
/// namespace F = torch::nn::functional;
|
| 41 |
+
/// F::normalize(input, F::NormalizeFuncOptions().p(1).dim(-1));
|
| 42 |
+
/// ```
|
| 43 |
+
inline Tensor normalize(
|
| 44 |
+
const Tensor& input,
|
| 45 |
+
NormalizeFuncOptions options = {}) {
|
| 46 |
+
return detail::normalize(
|
| 47 |
+
input, options.p(), options.dim(), options.eps(), options.out());
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// ============================================================================
|
| 51 |
+
|
| 52 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 53 |
+
namespace detail {
|
| 54 |
+
inline Tensor layer_norm(
|
| 55 |
+
const Tensor& input,
|
| 56 |
+
const std::vector<int64_t>& normalized_shape,
|
| 57 |
+
const Tensor& weight,
|
| 58 |
+
const Tensor& bias,
|
| 59 |
+
double eps) {
|
| 60 |
+
return torch::layer_norm(input, normalized_shape, weight, bias, eps);
|
| 61 |
+
}
|
| 62 |
+
} // namespace detail
|
| 63 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 64 |
+
|
| 65 |
+
/// See
|
| 66 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.layer_norm
|
| 67 |
+
/// about the exact behavior of this functional.
|
| 68 |
+
///
|
| 69 |
+
/// See the documentation for `torch::nn::functional::LayerNormFuncOptions`
|
| 70 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 71 |
+
///
|
| 72 |
+
/// Example:
|
| 73 |
+
/// ```
|
| 74 |
+
/// namespace F = torch::nn::functional;
|
| 75 |
+
/// F::layer_norm(input, F::LayerNormFuncOptions({2, 2}).eps(2e-5));
|
| 76 |
+
/// ```
|
| 77 |
+
inline Tensor layer_norm(
|
| 78 |
+
const Tensor& input,
|
| 79 |
+
const LayerNormFuncOptions& options) {
|
| 80 |
+
return detail::layer_norm(
|
| 81 |
+
input,
|
| 82 |
+
options.normalized_shape(),
|
| 83 |
+
options.weight(),
|
| 84 |
+
options.bias(),
|
| 85 |
+
options.eps());
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// ============================================================================
|
| 89 |
+
|
| 90 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 91 |
+
namespace detail {
|
| 92 |
+
inline Tensor local_response_norm(
|
| 93 |
+
const Tensor& input,
|
| 94 |
+
int64_t size,
|
| 95 |
+
double alpha,
|
| 96 |
+
double beta,
|
| 97 |
+
double k) {
|
| 98 |
+
auto dim = input.dim();
|
| 99 |
+
TORCH_CHECK(
|
| 100 |
+
dim >= 3,
|
| 101 |
+
"Expected 3D or higher dimensionality input (got ",
|
| 102 |
+
dim,
|
| 103 |
+
" dimensions)");
|
| 104 |
+
auto div = input.mul(input).unsqueeze(1);
|
| 105 |
+
if (dim == 3) {
|
| 106 |
+
div = detail::pad(
|
| 107 |
+
div,
|
| 108 |
+
/*pad=*/{0, 0, size / 2, (size - 1) / 2},
|
| 109 |
+
/*mode=*/torch::kConstant,
|
| 110 |
+
/*value=*/0);
|
| 111 |
+
div = detail::avg_pool2d(
|
| 112 |
+
div,
|
| 113 |
+
/*kernel_size=*/{size, 1},
|
| 114 |
+
/*stride=*/1,
|
| 115 |
+
/*padding=*/0,
|
| 116 |
+
/*ceil_mode=*/false,
|
| 117 |
+
/*count_include_pad=*/true,
|
| 118 |
+
/*divisor_override=*/c10::nullopt)
|
| 119 |
+
.squeeze(1);
|
| 120 |
+
} else {
|
| 121 |
+
auto sizes = input.sizes();
|
| 122 |
+
div = div.view({sizes[0], 1, sizes[1], sizes[2], -1});
|
| 123 |
+
div = detail::pad(
|
| 124 |
+
div,
|
| 125 |
+
/*pad=*/{0, 0, 0, 0, size / 2, (size - 1) / 2},
|
| 126 |
+
/*mode=*/torch::kConstant,
|
| 127 |
+
/*value=*/0);
|
| 128 |
+
div = detail::avg_pool3d(
|
| 129 |
+
div,
|
| 130 |
+
/*kernel_size=*/{size, 1, 1},
|
| 131 |
+
/*stride=*/1,
|
| 132 |
+
/*padding=*/0,
|
| 133 |
+
/*ceil_mode=*/false,
|
| 134 |
+
/*count_include_pad=*/true,
|
| 135 |
+
/*divisor_override=*/c10::nullopt)
|
| 136 |
+
.squeeze(1);
|
| 137 |
+
div = div.view(sizes);
|
| 138 |
+
}
|
| 139 |
+
div = div.mul(alpha).add(k).pow(beta);
|
| 140 |
+
return input / div;
|
| 141 |
+
}
|
| 142 |
+
} // namespace detail
|
| 143 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 144 |
+
|
| 145 |
+
/// See
|
| 146 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.local_response_norm
|
| 147 |
+
/// about the exact behavior of this functional.
|
| 148 |
+
///
|
| 149 |
+
/// See the documentation for
|
| 150 |
+
/// `torch::nn::functional::LocalResponseNormFuncOptions` class to learn what
|
| 151 |
+
/// optional arguments are supported for this functional.
|
| 152 |
+
///
|
| 153 |
+
/// Example:
|
| 154 |
+
/// ```
|
| 155 |
+
/// namespace F = torch::nn::functional;
|
| 156 |
+
/// F::local_response_norm(x, F::LocalResponseNormFuncOptions(2));
|
| 157 |
+
/// ```
|
| 158 |
+
inline Tensor local_response_norm(
|
| 159 |
+
const Tensor& input,
|
| 160 |
+
const LocalResponseNormFuncOptions& options) {
|
| 161 |
+
return detail::local_response_norm(
|
| 162 |
+
input, options.size(), options.alpha(), options.beta(), options.k());
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// ============================================================================
|
| 166 |
+
|
| 167 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 168 |
+
namespace detail {
|
| 169 |
+
inline Tensor group_norm(
|
| 170 |
+
const Tensor& input,
|
| 171 |
+
int64_t num_groups,
|
| 172 |
+
const Tensor& weight,
|
| 173 |
+
const Tensor& bias,
|
| 174 |
+
double eps) {
|
| 175 |
+
return torch::group_norm(
|
| 176 |
+
input,
|
| 177 |
+
num_groups,
|
| 178 |
+
weight,
|
| 179 |
+
bias,
|
| 180 |
+
eps,
|
| 181 |
+
at::globalContext().userEnabledCuDNN());
|
| 182 |
+
}
|
| 183 |
+
} // namespace detail
|
| 184 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 185 |
+
|
| 186 |
+
/// See
|
| 187 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.group_norm
|
| 188 |
+
/// about the exact behavior of this functional.
|
| 189 |
+
///
|
| 190 |
+
/// See the documentation for `torch::nn::functional::GroupNormFuncOptions`
|
| 191 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 192 |
+
///
|
| 193 |
+
/// Example:
|
| 194 |
+
/// ```
|
| 195 |
+
/// namespace F = torch::nn::functional;
|
| 196 |
+
/// F::group_norm(input, F::GroupNormFuncOptions(2).eps(2e-5));
|
| 197 |
+
/// ```
|
| 198 |
+
inline Tensor group_norm(
|
| 199 |
+
const Tensor& input,
|
| 200 |
+
const GroupNormFuncOptions& options) {
|
| 201 |
+
return detail::group_norm(
|
| 202 |
+
input,
|
| 203 |
+
options.num_groups(),
|
| 204 |
+
options.weight(),
|
| 205 |
+
options.bias(),
|
| 206 |
+
options.eps());
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
} // namespace functional
|
| 210 |
+
} // namespace nn
|
| 211 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/padding.h
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <ATen/PadNd.h>
|
| 4 |
+
#include <torch/nn/options/padding.h>
|
| 5 |
+
|
| 6 |
+
namespace torch {
|
| 7 |
+
namespace nn {
|
| 8 |
+
namespace functional {
|
| 9 |
+
|
| 10 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 11 |
+
namespace detail {
|
| 12 |
+
inline Tensor pad(
|
| 13 |
+
const Tensor& input,
|
| 14 |
+
IntArrayRef pad,
|
| 15 |
+
PadFuncOptions::mode_t mode,
|
| 16 |
+
double value) {
|
| 17 |
+
const auto mode_enum = [&] {
|
| 18 |
+
if (std::holds_alternative<enumtype::kConstant>(mode)) {
|
| 19 |
+
return at::padding_mode::constant;
|
| 20 |
+
} else if (std::holds_alternative<enumtype::kReflect>(mode)) {
|
| 21 |
+
return at::padding_mode::reflect;
|
| 22 |
+
} else if (std::holds_alternative<enumtype::kReplicate>(mode)) {
|
| 23 |
+
return at::padding_mode::replicate;
|
| 24 |
+
} else if (std::holds_alternative<enumtype::kCircular>(mode)) {
|
| 25 |
+
return at::padding_mode::circular;
|
| 26 |
+
}
|
| 27 |
+
TORCH_CHECK(false, "Unrecognised padding mode");
|
| 28 |
+
}();
|
| 29 |
+
|
| 30 |
+
c10::optional<double> fill_value;
|
| 31 |
+
if (value != 0.0) {
|
| 32 |
+
fill_value = value;
|
| 33 |
+
}
|
| 34 |
+
return at::_pad_enum(input, pad, static_cast<int64_t>(mode_enum), fill_value);
|
| 35 |
+
}
|
| 36 |
+
} // namespace detail
|
| 37 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 38 |
+
|
| 39 |
+
/// See
|
| 40 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.pad
|
| 41 |
+
/// about the exact behavior of this functional.
|
| 42 |
+
///
|
| 43 |
+
/// See the documentation for `torch::nn::functional::PadFuncOptions` class to
|
| 44 |
+
/// learn what optional arguments are supported for this functional.
|
| 45 |
+
///
|
| 46 |
+
/// Example:
|
| 47 |
+
/// ```
|
| 48 |
+
/// namespace F = torch::nn::functional;
|
| 49 |
+
/// F::pad(input, F::PadFuncOptions({1, 2, 2, 1, 1,
|
| 50 |
+
/// 2}).mode(torch::kReplicate));
|
| 51 |
+
/// ```
|
| 52 |
+
inline Tensor pad(const Tensor& input, const PadFuncOptions& options) {
|
| 53 |
+
return detail::pad(input, options.pad(), options.mode(), options.value());
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
} // namespace functional
|
| 57 |
+
} // namespace nn
|
| 58 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/pixelshuffle.h
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/options/pixelshuffle.h>
|
| 4 |
+
|
| 5 |
+
namespace torch {
|
| 6 |
+
namespace nn {
|
| 7 |
+
namespace functional {
|
| 8 |
+
|
| 9 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 10 |
+
namespace detail {
|
| 11 |
+
inline Tensor pixel_shuffle(const Tensor& input, int64_t upscale_factor) {
|
| 12 |
+
return torch::pixel_shuffle(input, upscale_factor);
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
inline Tensor pixel_unshuffle(const Tensor& input, int64_t downscale_factor) {
|
| 16 |
+
return torch::pixel_unshuffle(input, downscale_factor);
|
| 17 |
+
}
|
| 18 |
+
} // namespace detail
|
| 19 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 20 |
+
|
| 21 |
+
/// See
|
| 22 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.pixel_shuffle
|
| 23 |
+
/// about the exact behavior of this functional.
|
| 24 |
+
///
|
| 25 |
+
/// See the documentation for `torch::nn::functional::PixelShuffleFuncOptions`
|
| 26 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 27 |
+
///
|
| 28 |
+
/// Example:
|
| 29 |
+
/// ```
|
| 30 |
+
/// namespace F = torch::nn::functional;
|
| 31 |
+
/// F::pixel_shuffle(x, F::PixelShuffleFuncOptions(2));
|
| 32 |
+
/// ```
|
| 33 |
+
inline Tensor pixel_shuffle(
|
| 34 |
+
const Tensor& input,
|
| 35 |
+
const PixelShuffleFuncOptions& options) {
|
| 36 |
+
return detail::pixel_shuffle(input, options.upscale_factor());
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
inline Tensor pixel_unshuffle(
|
| 40 |
+
const Tensor& input,
|
| 41 |
+
const PixelUnshuffleFuncOptions& options) {
|
| 42 |
+
return detail::pixel_unshuffle(input, options.downscale_factor());
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
} // namespace functional
|
| 46 |
+
} // namespace nn
|
| 47 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/pooling.h
ADDED
|
@@ -0,0 +1,1098 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
#include <torch/nn/options/pooling.h>
|
| 7 |
+
|
| 8 |
+
namespace torch {
|
| 9 |
+
namespace nn {
|
| 10 |
+
namespace functional {
|
| 11 |
+
|
| 12 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 13 |
+
namespace detail {
|
| 14 |
+
inline Tensor avg_pool1d(
|
| 15 |
+
const Tensor& input,
|
| 16 |
+
ExpandingArray<1> kernel_size,
|
| 17 |
+
ExpandingArray<1> stride,
|
| 18 |
+
ExpandingArray<1> padding,
|
| 19 |
+
bool ceil_mode,
|
| 20 |
+
bool count_include_pad) {
|
| 21 |
+
return torch::avg_pool1d(
|
| 22 |
+
input, kernel_size, stride, padding, ceil_mode, count_include_pad);
|
| 23 |
+
}
|
| 24 |
+
} // namespace detail
|
| 25 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 26 |
+
|
| 27 |
+
/// See
|
| 28 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.avg_pool1d
|
| 29 |
+
/// about the exact behavior of this functional.
|
| 30 |
+
///
|
| 31 |
+
/// See the documentation for `torch::nn::functional::AvgPool1dFuncOptions`
|
| 32 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 33 |
+
///
|
| 34 |
+
/// Example:
|
| 35 |
+
/// ```
|
| 36 |
+
/// namespace F = torch::nn::functional;
|
| 37 |
+
/// F::avg_pool1d(x, F::AvgPool1dFuncOptions(3).stride(2));
|
| 38 |
+
/// ```
|
| 39 |
+
inline Tensor avg_pool1d(
|
| 40 |
+
const Tensor& input,
|
| 41 |
+
const AvgPool1dFuncOptions& options) {
|
| 42 |
+
return avg_pool1d(
|
| 43 |
+
input,
|
| 44 |
+
options.kernel_size(),
|
| 45 |
+
options.stride(),
|
| 46 |
+
options.padding(),
|
| 47 |
+
options.ceil_mode(),
|
| 48 |
+
options.count_include_pad());
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 52 |
+
namespace detail {
|
| 53 |
+
inline Tensor avg_pool2d(
|
| 54 |
+
const Tensor& input,
|
| 55 |
+
ExpandingArray<2> kernel_size,
|
| 56 |
+
ExpandingArray<2> stride,
|
| 57 |
+
ExpandingArray<2> padding,
|
| 58 |
+
bool ceil_mode,
|
| 59 |
+
bool count_include_pad,
|
| 60 |
+
c10::optional<int64_t> divisor_override) {
|
| 61 |
+
return torch::avg_pool2d(
|
| 62 |
+
input,
|
| 63 |
+
kernel_size,
|
| 64 |
+
stride,
|
| 65 |
+
padding,
|
| 66 |
+
ceil_mode,
|
| 67 |
+
count_include_pad,
|
| 68 |
+
divisor_override);
|
| 69 |
+
}
|
| 70 |
+
} // namespace detail
|
| 71 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 72 |
+
|
| 73 |
+
/// See
|
| 74 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.avg_pool2d
|
| 75 |
+
/// about the exact behavior of this functional.
|
| 76 |
+
///
|
| 77 |
+
/// See the documentation for `torch::nn::functional::AvgPool2dFuncOptions`
|
| 78 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 79 |
+
///
|
| 80 |
+
/// Example:
|
| 81 |
+
/// ```
|
| 82 |
+
/// namespace F = torch::nn::functional;
|
| 83 |
+
/// F::avg_pool2d(x, F::AvgPool2dFuncOptions(3).stride(2));
|
| 84 |
+
/// ```
|
| 85 |
+
inline Tensor avg_pool2d(
|
| 86 |
+
const Tensor& input,
|
| 87 |
+
const AvgPool2dFuncOptions& options) {
|
| 88 |
+
return detail::avg_pool2d(
|
| 89 |
+
input,
|
| 90 |
+
options.kernel_size(),
|
| 91 |
+
options.stride(),
|
| 92 |
+
options.padding(),
|
| 93 |
+
options.ceil_mode(),
|
| 94 |
+
options.count_include_pad(),
|
| 95 |
+
options.divisor_override());
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 99 |
+
namespace detail {
|
| 100 |
+
inline Tensor avg_pool3d(
|
| 101 |
+
const Tensor& input,
|
| 102 |
+
ExpandingArray<3> kernel_size,
|
| 103 |
+
ExpandingArray<3> stride,
|
| 104 |
+
ExpandingArray<3> padding,
|
| 105 |
+
bool ceil_mode,
|
| 106 |
+
bool count_include_pad,
|
| 107 |
+
c10::optional<int64_t> divisor_override) {
|
| 108 |
+
return torch::avg_pool3d(
|
| 109 |
+
input,
|
| 110 |
+
kernel_size,
|
| 111 |
+
stride,
|
| 112 |
+
padding,
|
| 113 |
+
ceil_mode,
|
| 114 |
+
count_include_pad,
|
| 115 |
+
divisor_override);
|
| 116 |
+
}
|
| 117 |
+
} // namespace detail
|
| 118 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 119 |
+
|
| 120 |
+
/// See
|
| 121 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.avg_pool3d
|
| 122 |
+
/// about the exact behavior of this functional.
|
| 123 |
+
///
|
| 124 |
+
/// See the documentation for `torch::nn::functional::AvgPool3dFuncOptions`
|
| 125 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 126 |
+
///
|
| 127 |
+
/// Example:
|
| 128 |
+
/// ```
|
| 129 |
+
/// namespace F = torch::nn::functional;
|
| 130 |
+
/// F::avg_pool3d(x, F::AvgPool3dFuncOptions(3).stride(2));
|
| 131 |
+
/// ```
|
| 132 |
+
inline Tensor avg_pool3d(
|
| 133 |
+
const Tensor& input,
|
| 134 |
+
const AvgPool3dFuncOptions& options) {
|
| 135 |
+
return detail::avg_pool3d(
|
| 136 |
+
input,
|
| 137 |
+
options.kernel_size(),
|
| 138 |
+
options.stride(),
|
| 139 |
+
options.padding(),
|
| 140 |
+
options.ceil_mode(),
|
| 141 |
+
options.count_include_pad(),
|
| 142 |
+
options.divisor_override());
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
// ============================================================================
|
| 146 |
+
|
| 147 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 148 |
+
namespace detail {
|
| 149 |
+
inline Tensor max_pool1d(
|
| 150 |
+
const Tensor& input,
|
| 151 |
+
ExpandingArray<1> kernel_size,
|
| 152 |
+
ExpandingArray<1> stride,
|
| 153 |
+
ExpandingArray<1> padding,
|
| 154 |
+
ExpandingArray<1> dilation,
|
| 155 |
+
bool ceil_mode) {
|
| 156 |
+
return torch::max_pool1d(
|
| 157 |
+
input, kernel_size, stride, padding, dilation, ceil_mode);
|
| 158 |
+
}
|
| 159 |
+
} // namespace detail
|
| 160 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 161 |
+
|
| 162 |
+
/// See
|
| 163 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.max_pool1d
|
| 164 |
+
/// about the exact behavior of this functional.
|
| 165 |
+
///
|
| 166 |
+
/// See the documentation for `torch::nn::functional::MaxPool1dFuncOptions`
|
| 167 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 168 |
+
///
|
| 169 |
+
/// Example:
|
| 170 |
+
/// ```
|
| 171 |
+
/// namespace F = torch::nn::functional;
|
| 172 |
+
/// F::max_pool1d(x, F::MaxPool1dFuncOptions(3).stride(2));
|
| 173 |
+
/// ```
|
| 174 |
+
inline Tensor max_pool1d(
|
| 175 |
+
const Tensor& input,
|
| 176 |
+
const MaxPool1dFuncOptions& options) {
|
| 177 |
+
return detail::max_pool1d(
|
| 178 |
+
input,
|
| 179 |
+
options.kernel_size(),
|
| 180 |
+
options.stride(),
|
| 181 |
+
options.padding(),
|
| 182 |
+
options.dilation(),
|
| 183 |
+
options.ceil_mode());
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 187 |
+
namespace detail {
|
| 188 |
+
inline std::tuple<Tensor, Tensor> max_pool1d_with_indices(
|
| 189 |
+
const Tensor& input,
|
| 190 |
+
ExpandingArray<1> kernel_size,
|
| 191 |
+
ExpandingArray<1> stride,
|
| 192 |
+
ExpandingArray<1> padding,
|
| 193 |
+
ExpandingArray<1> dilation,
|
| 194 |
+
bool ceil_mode) {
|
| 195 |
+
return torch::max_pool1d_with_indices(
|
| 196 |
+
input, kernel_size, stride, padding, dilation, ceil_mode);
|
| 197 |
+
}
|
| 198 |
+
} // namespace detail
|
| 199 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 200 |
+
|
| 201 |
+
/// See the documentation for `torch::nn::functional::MaxPool1dFuncOptions`
|
| 202 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 203 |
+
///
|
| 204 |
+
/// Example:
|
| 205 |
+
/// ```
|
| 206 |
+
/// namespace F = torch::nn::functional;
|
| 207 |
+
/// F::max_pool1d_with_indices(x, F::MaxPool1dFuncOptions(3).stride(2));
|
| 208 |
+
/// ```
|
| 209 |
+
inline std::tuple<Tensor, Tensor> max_pool1d_with_indices(
|
| 210 |
+
const Tensor& input,
|
| 211 |
+
const MaxPool1dFuncOptions& options) {
|
| 212 |
+
return detail::max_pool1d_with_indices(
|
| 213 |
+
input,
|
| 214 |
+
options.kernel_size(),
|
| 215 |
+
options.stride(),
|
| 216 |
+
options.padding(),
|
| 217 |
+
options.dilation(),
|
| 218 |
+
options.ceil_mode());
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 222 |
+
namespace detail {
|
| 223 |
+
inline Tensor max_pool2d(
|
| 224 |
+
const Tensor& input,
|
| 225 |
+
ExpandingArray<2> kernel_size,
|
| 226 |
+
ExpandingArray<2> stride,
|
| 227 |
+
ExpandingArray<2> padding,
|
| 228 |
+
ExpandingArray<2> dilation,
|
| 229 |
+
bool ceil_mode) {
|
| 230 |
+
return torch::max_pool2d(
|
| 231 |
+
input, kernel_size, stride, padding, dilation, ceil_mode);
|
| 232 |
+
}
|
| 233 |
+
} // namespace detail
|
| 234 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 235 |
+
|
| 236 |
+
/// See
|
| 237 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.max_pool2d
|
| 238 |
+
/// about the exact behavior of this functional.
|
| 239 |
+
///
|
| 240 |
+
/// See the documentation for `torch::nn::functional::MaxPool2dFuncOptions`
|
| 241 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 242 |
+
///
|
| 243 |
+
/// Example:
|
| 244 |
+
/// ```
|
| 245 |
+
/// namespace F = torch::nn::functional;
|
| 246 |
+
/// F::max_pool2d(x, F::MaxPool2dFuncOptions(3).stride(2));
|
| 247 |
+
/// ```
|
| 248 |
+
inline Tensor max_pool2d(
|
| 249 |
+
const Tensor& input,
|
| 250 |
+
const MaxPool2dFuncOptions& options) {
|
| 251 |
+
return detail::max_pool2d(
|
| 252 |
+
input,
|
| 253 |
+
options.kernel_size(),
|
| 254 |
+
options.stride(),
|
| 255 |
+
options.padding(),
|
| 256 |
+
options.dilation(),
|
| 257 |
+
options.ceil_mode());
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 261 |
+
namespace detail {
|
| 262 |
+
inline std::tuple<Tensor, Tensor> max_pool2d_with_indices(
|
| 263 |
+
const Tensor& input,
|
| 264 |
+
ExpandingArray<2> kernel_size,
|
| 265 |
+
ExpandingArray<2> stride,
|
| 266 |
+
ExpandingArray<2> padding,
|
| 267 |
+
ExpandingArray<2> dilation,
|
| 268 |
+
bool ceil_mode) {
|
| 269 |
+
return torch::max_pool2d_with_indices(
|
| 270 |
+
input, kernel_size, stride, padding, dilation, ceil_mode);
|
| 271 |
+
}
|
| 272 |
+
} // namespace detail
|
| 273 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 274 |
+
|
| 275 |
+
/// See the documentation for `torch::nn::functional::MaxPool2dFuncOptions`
|
| 276 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 277 |
+
///
|
| 278 |
+
/// Example:
|
| 279 |
+
/// ```
|
| 280 |
+
/// namespace F = torch::nn::functional;
|
| 281 |
+
/// F::max_pool2d_with_indices(x, F::MaxPool2dFuncOptions(3).stride(2));
|
| 282 |
+
/// ```
|
| 283 |
+
inline std::tuple<Tensor, Tensor> max_pool2d_with_indices(
|
| 284 |
+
const Tensor& input,
|
| 285 |
+
const MaxPool2dFuncOptions& options) {
|
| 286 |
+
return detail::max_pool2d_with_indices(
|
| 287 |
+
input,
|
| 288 |
+
options.kernel_size(),
|
| 289 |
+
options.stride(),
|
| 290 |
+
options.padding(),
|
| 291 |
+
options.dilation(),
|
| 292 |
+
options.ceil_mode());
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 296 |
+
namespace detail {
|
| 297 |
+
inline Tensor max_pool3d(
|
| 298 |
+
const Tensor& input,
|
| 299 |
+
ExpandingArray<3> kernel_size,
|
| 300 |
+
ExpandingArray<3> stride,
|
| 301 |
+
ExpandingArray<3> padding,
|
| 302 |
+
ExpandingArray<3> dilation,
|
| 303 |
+
bool ceil_mode) {
|
| 304 |
+
return torch::max_pool3d(
|
| 305 |
+
input, kernel_size, stride, padding, dilation, ceil_mode);
|
| 306 |
+
}
|
| 307 |
+
} // namespace detail
|
| 308 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 309 |
+
|
| 310 |
+
/// See
|
| 311 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.max_pool3d
|
| 312 |
+
/// about the exact behavior of this functional.
|
| 313 |
+
///
|
| 314 |
+
/// See the documentation for `torch::nn::functional::MaxPool3dFuncOptions`
|
| 315 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 316 |
+
///
|
| 317 |
+
/// Example:
|
| 318 |
+
/// ```
|
| 319 |
+
/// namespace F = torch::nn::functional;
|
| 320 |
+
/// F::max_pool3d(x, F::MaxPool3dFuncOptions(3).stride(2));
|
| 321 |
+
/// ```
|
| 322 |
+
inline Tensor max_pool3d(
|
| 323 |
+
const Tensor& input,
|
| 324 |
+
const MaxPool3dFuncOptions& options) {
|
| 325 |
+
return detail::max_pool3d(
|
| 326 |
+
input,
|
| 327 |
+
options.kernel_size(),
|
| 328 |
+
options.stride(),
|
| 329 |
+
options.padding(),
|
| 330 |
+
options.dilation(),
|
| 331 |
+
options.ceil_mode());
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 335 |
+
namespace detail {
|
| 336 |
+
inline std::tuple<Tensor, Tensor> max_pool3d_with_indices(
|
| 337 |
+
const Tensor& input,
|
| 338 |
+
ExpandingArray<3> kernel_size,
|
| 339 |
+
ExpandingArray<3> stride,
|
| 340 |
+
ExpandingArray<3> padding,
|
| 341 |
+
ExpandingArray<3> dilation,
|
| 342 |
+
bool ceil_mode) {
|
| 343 |
+
return torch::max_pool3d_with_indices(
|
| 344 |
+
input, kernel_size, stride, padding, dilation, ceil_mode);
|
| 345 |
+
}
|
| 346 |
+
} // namespace detail
|
| 347 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 348 |
+
|
| 349 |
+
/// See the documentation for `torch::nn::functional::MaxPool3dFuncOptions`
|
| 350 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 351 |
+
///
|
| 352 |
+
/// Example:
|
| 353 |
+
/// ```
|
| 354 |
+
/// namespace F = torch::nn::functional;
|
| 355 |
+
/// F::max_pool3d_with_indices(x, F::MaxPool3dFuncOptions(3).stride(2));
|
| 356 |
+
/// ```
|
| 357 |
+
inline std::tuple<Tensor, Tensor> max_pool3d_with_indices(
|
| 358 |
+
const Tensor& input,
|
| 359 |
+
const MaxPool3dFuncOptions& options) {
|
| 360 |
+
return detail::max_pool3d_with_indices(
|
| 361 |
+
input,
|
| 362 |
+
options.kernel_size(),
|
| 363 |
+
options.stride(),
|
| 364 |
+
options.padding(),
|
| 365 |
+
options.dilation(),
|
| 366 |
+
options.ceil_mode());
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
// ============================================================================
|
| 370 |
+
|
| 371 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 372 |
+
namespace detail {
|
| 373 |
+
inline std::tuple<Tensor, Tensor> adaptive_max_pool1d_with_indices(
|
| 374 |
+
const Tensor& input,
|
| 375 |
+
ExpandingArray<1> output_size) {
|
| 376 |
+
return torch::adaptive_max_pool1d(input, output_size);
|
| 377 |
+
}
|
| 378 |
+
} // namespace detail
|
| 379 |
+
|
| 380 |
+
/// See the documentation for
|
| 381 |
+
/// `torch::nn::functional::AdaptiveMaxPool1dFuncOptions` class to learn what
|
| 382 |
+
/// optional arguments are supported for this functional.
|
| 383 |
+
///
|
| 384 |
+
/// Example:
|
| 385 |
+
/// ```
|
| 386 |
+
/// namespace F = torch::nn::functional;
|
| 387 |
+
/// F::adaptive_max_pool1d_with_indices(x, F::AdaptiveMaxPool1dFuncOptions(3));
|
| 388 |
+
/// ```
|
| 389 |
+
inline std::tuple<Tensor, Tensor> adaptive_max_pool1d_with_indices(
|
| 390 |
+
const Tensor& input,
|
| 391 |
+
const AdaptiveMaxPool1dFuncOptions& options) {
|
| 392 |
+
return detail::adaptive_max_pool1d_with_indices(input, options.output_size());
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
namespace detail {
|
| 396 |
+
inline Tensor adaptive_max_pool1d(
|
| 397 |
+
const Tensor& input,
|
| 398 |
+
ExpandingArray<1> output_size) {
|
| 399 |
+
return std::get<0>(adaptive_max_pool1d_with_indices(input, output_size));
|
| 400 |
+
}
|
| 401 |
+
} // namespace detail
|
| 402 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 403 |
+
|
| 404 |
+
/// See
|
| 405 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.adaptive_max_pool1d
|
| 406 |
+
/// about the exact behavior of this functional.
|
| 407 |
+
///
|
| 408 |
+
/// See the documentation for
|
| 409 |
+
/// `torch::nn::functional::AdaptiveMaxPool1dFuncOptions` class to learn what
|
| 410 |
+
/// optional arguments are supported for this functional.
|
| 411 |
+
///
|
| 412 |
+
/// Example:
|
| 413 |
+
/// ```
|
| 414 |
+
/// namespace F = torch::nn::functional;
|
| 415 |
+
/// F::adaptive_max_pool1d(x, F::AdaptiveMaxPool1dFuncOptions(3));
|
| 416 |
+
/// ```
|
| 417 |
+
inline Tensor adaptive_max_pool1d(
|
| 418 |
+
const Tensor& input,
|
| 419 |
+
const AdaptiveMaxPool1dFuncOptions& options) {
|
| 420 |
+
return detail::adaptive_max_pool1d(input, options.output_size());
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 424 |
+
namespace detail {
|
| 425 |
+
inline std::tuple<Tensor, Tensor> adaptive_max_pool2d_with_indices(
|
| 426 |
+
const Tensor& input,
|
| 427 |
+
ExpandingArrayWithOptionalElem<2> output_size) {
|
| 428 |
+
auto output_size_ =
|
| 429 |
+
torch::nn::modules::utils::_list_with_default(output_size, input.sizes());
|
| 430 |
+
return torch::adaptive_max_pool2d(input, output_size_);
|
| 431 |
+
}
|
| 432 |
+
} // namespace detail
|
| 433 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 434 |
+
|
| 435 |
+
/// See the documentation for
|
| 436 |
+
/// `torch::nn::functional::AdaptiveMaxPool2dFuncOptions` class to learn what
|
| 437 |
+
/// optional arguments are supported for this functional.
|
| 438 |
+
///
|
| 439 |
+
/// Example:
|
| 440 |
+
/// ```
|
| 441 |
+
/// namespace F = torch::nn::functional;
|
| 442 |
+
/// F::adaptive_max_pool2d_with_indices(x, F::AdaptiveMaxPool2dFuncOptions(3));
|
| 443 |
+
/// ```
|
| 444 |
+
inline std::tuple<Tensor, Tensor> adaptive_max_pool2d_with_indices(
|
| 445 |
+
const Tensor& input,
|
| 446 |
+
const AdaptiveMaxPool2dFuncOptions& options) {
|
| 447 |
+
return detail::adaptive_max_pool2d_with_indices(input, options.output_size());
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 451 |
+
namespace detail {
|
| 452 |
+
inline Tensor adaptive_max_pool2d(
|
| 453 |
+
const Tensor& input,
|
| 454 |
+
ExpandingArrayWithOptionalElem<2> output_size) {
|
| 455 |
+
return std::get<0>(adaptive_max_pool2d_with_indices(input, output_size));
|
| 456 |
+
}
|
| 457 |
+
} // namespace detail
|
| 458 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 459 |
+
|
| 460 |
+
/// See
|
| 461 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.adaptive_max_pool2d
|
| 462 |
+
/// about the exact behavior of this functional.
|
| 463 |
+
///
|
| 464 |
+
/// See the documentation for
|
| 465 |
+
/// `torch::nn::functional::AdaptiveMaxPool2dFuncOptions` class to learn what
|
| 466 |
+
/// optional arguments are supported for this functional.
|
| 467 |
+
///
|
| 468 |
+
/// Example:
|
| 469 |
+
/// ```
|
| 470 |
+
/// namespace F = torch::nn::functional;
|
| 471 |
+
/// F::adaptive_max_pool2d(x, F::AdaptiveMaxPool2dFuncOptions(3));
|
| 472 |
+
/// ```
|
| 473 |
+
inline Tensor adaptive_max_pool2d(
|
| 474 |
+
const Tensor& input,
|
| 475 |
+
const AdaptiveMaxPool2dFuncOptions& options) {
|
| 476 |
+
return detail::adaptive_max_pool2d(input, options.output_size());
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 480 |
+
namespace detail {
|
| 481 |
+
inline std::tuple<Tensor, Tensor> adaptive_max_pool3d_with_indices(
|
| 482 |
+
const Tensor& input,
|
| 483 |
+
ExpandingArrayWithOptionalElem<3> output_size) {
|
| 484 |
+
auto output_size_ =
|
| 485 |
+
torch::nn::modules::utils::_list_with_default(output_size, input.sizes());
|
| 486 |
+
return torch::adaptive_max_pool3d(input, output_size_);
|
| 487 |
+
}
|
| 488 |
+
} // namespace detail
|
| 489 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 490 |
+
|
| 491 |
+
/// See the documentation for
|
| 492 |
+
/// `torch::nn::functional::AdaptiveMaxPool3dFuncOptions` class to learn what
|
| 493 |
+
/// optional arguments are supported for this functional.
|
| 494 |
+
///
|
| 495 |
+
/// Example:
|
| 496 |
+
/// ```
|
| 497 |
+
/// namespace F = torch::nn::functional;
|
| 498 |
+
/// F::adaptive_max_pool3d_with_indices(x, F::AdaptiveMaxPool3dFuncOptions(3));
|
| 499 |
+
/// ```
|
| 500 |
+
inline std::tuple<Tensor, Tensor> adaptive_max_pool3d_with_indices(
|
| 501 |
+
const Tensor& input,
|
| 502 |
+
const AdaptiveMaxPool3dFuncOptions& options) {
|
| 503 |
+
return detail::adaptive_max_pool3d_with_indices(input, options.output_size());
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 507 |
+
namespace detail {
|
| 508 |
+
inline Tensor adaptive_max_pool3d(
|
| 509 |
+
const Tensor& input,
|
| 510 |
+
ExpandingArrayWithOptionalElem<3> output_size) {
|
| 511 |
+
return std::get<0>(adaptive_max_pool3d_with_indices(input, output_size));
|
| 512 |
+
}
|
| 513 |
+
} // namespace detail
|
| 514 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 515 |
+
|
| 516 |
+
/// See
|
| 517 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.adaptive_max_pool3d
|
| 518 |
+
/// about the exact behavior of this functional.
|
| 519 |
+
///
|
| 520 |
+
/// See the documentation for
|
| 521 |
+
/// `torch::nn::functional::AdaptiveMaxPool3dFuncOptions` class to learn what
|
| 522 |
+
/// optional arguments are supported for this functional.
|
| 523 |
+
///
|
| 524 |
+
/// Example:
|
| 525 |
+
/// ```
|
| 526 |
+
/// namespace F = torch::nn::functional;
|
| 527 |
+
/// F::adaptive_max_pool3d(x, F::AdaptiveMaxPool3dFuncOptions(3));
|
| 528 |
+
/// ```
|
| 529 |
+
inline Tensor adaptive_max_pool3d(
|
| 530 |
+
const Tensor& input,
|
| 531 |
+
const AdaptiveMaxPool3dFuncOptions& options) {
|
| 532 |
+
return detail::adaptive_max_pool3d(input, options.output_size());
|
| 533 |
+
}
|
| 534 |
+
|
| 535 |
+
// ============================================================================
|
| 536 |
+
|
| 537 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 538 |
+
namespace detail {
|
| 539 |
+
inline Tensor adaptive_avg_pool1d(
|
| 540 |
+
const Tensor& input,
|
| 541 |
+
ExpandingArray<1> output_size) {
|
| 542 |
+
return torch::adaptive_avg_pool1d(input, output_size);
|
| 543 |
+
}
|
| 544 |
+
} // namespace detail
|
| 545 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 546 |
+
|
| 547 |
+
/// See
|
| 548 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.adaptive_avg_pool1d
|
| 549 |
+
/// about the exact behavior of this functional.
|
| 550 |
+
///
|
| 551 |
+
/// See the documentation for
|
| 552 |
+
/// `torch::nn::functional::AdaptiveAvgPool1dFuncOptions` class to learn what
|
| 553 |
+
/// optional arguments are supported for this functional.
|
| 554 |
+
///
|
| 555 |
+
/// Example:
|
| 556 |
+
/// ```
|
| 557 |
+
/// namespace F = torch::nn::functional;
|
| 558 |
+
/// F::adaptive_avg_pool1d(x, F::AdaptiveAvgPool1dFuncOptions(3));
|
| 559 |
+
/// ```
|
| 560 |
+
inline Tensor adaptive_avg_pool1d(
|
| 561 |
+
const Tensor& input,
|
| 562 |
+
const AdaptiveAvgPool1dFuncOptions& options) {
|
| 563 |
+
return detail::adaptive_avg_pool1d(input, options.output_size());
|
| 564 |
+
}
|
| 565 |
+
|
| 566 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 567 |
+
namespace detail {
|
| 568 |
+
inline Tensor adaptive_avg_pool2d(
|
| 569 |
+
const Tensor& input,
|
| 570 |
+
ExpandingArrayWithOptionalElem<2> output_size) {
|
| 571 |
+
auto output_size_ =
|
| 572 |
+
torch::nn::modules::utils::_list_with_default(output_size, input.sizes());
|
| 573 |
+
return torch::adaptive_avg_pool2d(input, output_size_);
|
| 574 |
+
}
|
| 575 |
+
} // namespace detail
|
| 576 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 577 |
+
|
| 578 |
+
/// See
|
| 579 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.adaptive_avg_pool2d
|
| 580 |
+
/// about the exact behavior of this functional.
|
| 581 |
+
///
|
| 582 |
+
/// See the documentation for
|
| 583 |
+
/// `torch::nn::functional::AdaptiveAvgPool2dFuncOptions` class to learn what
|
| 584 |
+
/// optional arguments are supported for this functional.
|
| 585 |
+
///
|
| 586 |
+
/// Example:
|
| 587 |
+
/// ```
|
| 588 |
+
/// namespace F = torch::nn::functional;
|
| 589 |
+
/// F::adaptive_avg_pool2d(x, F::AdaptiveAvgPool2dFuncOptions(3));
|
| 590 |
+
/// ```
|
| 591 |
+
inline Tensor adaptive_avg_pool2d(
|
| 592 |
+
const Tensor& input,
|
| 593 |
+
const AdaptiveAvgPool2dFuncOptions& options) {
|
| 594 |
+
return detail::adaptive_avg_pool2d(input, options.output_size());
|
| 595 |
+
}
|
| 596 |
+
|
| 597 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 598 |
+
namespace detail {
|
| 599 |
+
inline Tensor adaptive_avg_pool3d(
|
| 600 |
+
const Tensor& input,
|
| 601 |
+
ExpandingArrayWithOptionalElem<3> output_size) {
|
| 602 |
+
auto output_size_ =
|
| 603 |
+
torch::nn::modules::utils::_list_with_default(output_size, input.sizes());
|
| 604 |
+
return torch::adaptive_avg_pool3d(input, output_size_);
|
| 605 |
+
}
|
| 606 |
+
} // namespace detail
|
| 607 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 608 |
+
|
| 609 |
+
/// See
|
| 610 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.adaptive_avg_pool3d
|
| 611 |
+
/// about the exact behavior of this functional.
|
| 612 |
+
///
|
| 613 |
+
/// See the documentation for
|
| 614 |
+
/// `torch::nn::functional::AdaptiveAvgPool3dFuncOptions` class to learn what
|
| 615 |
+
/// optional arguments are supported for this functional.
|
| 616 |
+
///
|
| 617 |
+
/// Example:
|
| 618 |
+
/// ```
|
| 619 |
+
/// namespace F = torch::nn::functional;
|
| 620 |
+
/// F::adaptive_avg_pool3d(x, F::AdaptiveAvgPool3dFuncOptions(3));
|
| 621 |
+
/// ```
|
| 622 |
+
inline Tensor adaptive_avg_pool3d(
|
| 623 |
+
const Tensor& input,
|
| 624 |
+
const AdaptiveAvgPool3dFuncOptions& options) {
|
| 625 |
+
return detail::adaptive_avg_pool3d(input, options.output_size());
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
// ============================================================================
|
| 629 |
+
|
| 630 |
+
inline std::vector<int64_t> _unpool_output_size(
|
| 631 |
+
const Tensor& input,
|
| 632 |
+
const IntArrayRef& kernel_size,
|
| 633 |
+
const IntArrayRef& stride,
|
| 634 |
+
const IntArrayRef& padding,
|
| 635 |
+
const c10::optional<std::vector<int64_t>>& output_size) {
|
| 636 |
+
auto input_size = input.sizes();
|
| 637 |
+
std::vector<int64_t> default_size;
|
| 638 |
+
for (const auto d : c10::irange(kernel_size.size())) {
|
| 639 |
+
default_size.push_back(
|
| 640 |
+
(input_size[input_size.size() - kernel_size.size() + d] - 1) *
|
| 641 |
+
stride[d] +
|
| 642 |
+
kernel_size[d] - 2 * padding[d]);
|
| 643 |
+
}
|
| 644 |
+
if (!output_size) {
|
| 645 |
+
return default_size;
|
| 646 |
+
} else {
|
| 647 |
+
std::vector<int64_t> output_size_;
|
| 648 |
+
if (output_size->size() == kernel_size.size() + 2) {
|
| 649 |
+
output_size_ = IntArrayRef(*output_size).slice(2).vec();
|
| 650 |
+
}
|
| 651 |
+
if (output_size_.size() != kernel_size.size()) {
|
| 652 |
+
TORCH_CHECK(
|
| 653 |
+
false,
|
| 654 |
+
"output_size should be a sequence containing ",
|
| 655 |
+
kernel_size.size(),
|
| 656 |
+
" or ",
|
| 657 |
+
kernel_size.size() + 2,
|
| 658 |
+
" elements, but it has a length of '",
|
| 659 |
+
output_size_.size(),
|
| 660 |
+
"'");
|
| 661 |
+
}
|
| 662 |
+
for (const auto d : c10::irange(kernel_size.size())) {
|
| 663 |
+
const auto min_size = default_size[d] - stride[d];
|
| 664 |
+
const auto max_size = default_size[d] + stride[d];
|
| 665 |
+
if (!(min_size <= output_size_[d] && output_size_[d] <= max_size)) {
|
| 666 |
+
TORCH_CHECK(
|
| 667 |
+
false,
|
| 668 |
+
"invalid output_size ",
|
| 669 |
+
output_size_,
|
| 670 |
+
" (dim ",
|
| 671 |
+
d,
|
| 672 |
+
" must be between ",
|
| 673 |
+
min_size,
|
| 674 |
+
" and ",
|
| 675 |
+
max_size,
|
| 676 |
+
")");
|
| 677 |
+
}
|
| 678 |
+
}
|
| 679 |
+
return output_size_;
|
| 680 |
+
}
|
| 681 |
+
}
|
| 682 |
+
|
| 683 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 684 |
+
namespace detail {
|
| 685 |
+
inline Tensor max_unpool1d(
|
| 686 |
+
const Tensor& input,
|
| 687 |
+
const Tensor& indices,
|
| 688 |
+
ExpandingArray<1> kernel_size,
|
| 689 |
+
ExpandingArray<1> stride,
|
| 690 |
+
ExpandingArray<1> padding,
|
| 691 |
+
const c10::optional<std::vector<int64_t>>& output_size) {
|
| 692 |
+
auto output_size_ =
|
| 693 |
+
_unpool_output_size(input, kernel_size, stride, padding, output_size);
|
| 694 |
+
output_size_.push_back(1);
|
| 695 |
+
return torch::max_unpool2d(
|
| 696 |
+
input.unsqueeze(-1), indices.unsqueeze(-1), output_size_)
|
| 697 |
+
.squeeze(-1);
|
| 698 |
+
}
|
| 699 |
+
} // namespace detail
|
| 700 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 701 |
+
|
| 702 |
+
/// See
|
| 703 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.max_unpool1d
|
| 704 |
+
/// about the exact behavior of this functional.
|
| 705 |
+
///
|
| 706 |
+
/// See the documentation for `torch::nn::functional::MaxUnpool1dFuncOptions`
|
| 707 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 708 |
+
///
|
| 709 |
+
/// Example:
|
| 710 |
+
/// ```
|
| 711 |
+
/// namespace F = torch::nn::functional;
|
| 712 |
+
/// F::max_unpool1d(x, indices,
|
| 713 |
+
/// F::MaxUnpool1dFuncOptions(3).stride(2).padding(1));
|
| 714 |
+
/// ```
|
| 715 |
+
inline Tensor max_unpool1d(
|
| 716 |
+
const Tensor& input,
|
| 717 |
+
const Tensor& indices,
|
| 718 |
+
const MaxUnpool1dFuncOptions& options) {
|
| 719 |
+
return detail::max_unpool1d(
|
| 720 |
+
input,
|
| 721 |
+
indices,
|
| 722 |
+
options.kernel_size(),
|
| 723 |
+
options.stride(),
|
| 724 |
+
options.padding(),
|
| 725 |
+
options.output_size());
|
| 726 |
+
}
|
| 727 |
+
|
| 728 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 729 |
+
namespace detail {
|
| 730 |
+
inline Tensor max_unpool2d(
|
| 731 |
+
const Tensor& input,
|
| 732 |
+
const Tensor& indices,
|
| 733 |
+
ExpandingArray<2> kernel_size,
|
| 734 |
+
ExpandingArray<2> stride,
|
| 735 |
+
ExpandingArray<2> padding,
|
| 736 |
+
const c10::optional<std::vector<int64_t>>& output_size) {
|
| 737 |
+
auto output_size_ =
|
| 738 |
+
_unpool_output_size(input, kernel_size, stride, padding, output_size);
|
| 739 |
+
|
| 740 |
+
return torch::max_unpool2d(input, indices, output_size_);
|
| 741 |
+
}
|
| 742 |
+
} // namespace detail
|
| 743 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 744 |
+
|
| 745 |
+
/// See
|
| 746 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.max_unpool2d
|
| 747 |
+
/// about the exact behavior of this functional.
|
| 748 |
+
///
|
| 749 |
+
/// See the documentation for `torch::nn::functional::MaxUnpool2dFuncOptions`
|
| 750 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 751 |
+
///
|
| 752 |
+
/// Example:
|
| 753 |
+
/// ```
|
| 754 |
+
/// namespace F = torch::nn::functional;
|
| 755 |
+
/// F::max_unpool2d(x, indices,
|
| 756 |
+
/// F::MaxUnpool2dFuncOptions(3).stride(2).padding(1));
|
| 757 |
+
/// ```
|
| 758 |
+
inline Tensor max_unpool2d(
|
| 759 |
+
const Tensor& input,
|
| 760 |
+
const Tensor& indices,
|
| 761 |
+
const MaxUnpool2dFuncOptions& options) {
|
| 762 |
+
return detail::max_unpool2d(
|
| 763 |
+
input,
|
| 764 |
+
indices,
|
| 765 |
+
options.kernel_size(),
|
| 766 |
+
options.stride(),
|
| 767 |
+
options.padding(),
|
| 768 |
+
options.output_size());
|
| 769 |
+
}
|
| 770 |
+
|
| 771 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 772 |
+
namespace detail {
|
| 773 |
+
inline Tensor max_unpool3d(
|
| 774 |
+
const Tensor& input,
|
| 775 |
+
const Tensor& indices,
|
| 776 |
+
ExpandingArray<3> kernel_size,
|
| 777 |
+
ExpandingArray<3> stride,
|
| 778 |
+
ExpandingArray<3> padding,
|
| 779 |
+
const c10::optional<std::vector<int64_t>>& output_size) {
|
| 780 |
+
auto output_size_ =
|
| 781 |
+
_unpool_output_size(input, kernel_size, stride, padding, output_size);
|
| 782 |
+
|
| 783 |
+
return torch::max_unpool3d(input, indices, output_size_, stride, padding);
|
| 784 |
+
}
|
| 785 |
+
} // namespace detail
|
| 786 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 787 |
+
|
| 788 |
+
/// See
|
| 789 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.max_unpool3d
|
| 790 |
+
/// about the exact behavior of this functional.
|
| 791 |
+
///
|
| 792 |
+
/// See the documentation for `torch::nn::functional::MaxUnpool3dFuncOptions`
|
| 793 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 794 |
+
///
|
| 795 |
+
/// Example:
|
| 796 |
+
/// ```
|
| 797 |
+
/// namespace F = torch::nn::functional;
|
| 798 |
+
/// F::max_unpool3d(x, indices, F::MaxUnpool3dFuncOptions(3));
|
| 799 |
+
/// ```
|
| 800 |
+
inline Tensor max_unpool3d(
|
| 801 |
+
const Tensor& input,
|
| 802 |
+
const Tensor& indices,
|
| 803 |
+
const MaxUnpool3dFuncOptions& options) {
|
| 804 |
+
return detail::max_unpool3d(
|
| 805 |
+
input,
|
| 806 |
+
indices,
|
| 807 |
+
options.kernel_size(),
|
| 808 |
+
options.stride(),
|
| 809 |
+
options.padding(),
|
| 810 |
+
options.output_size());
|
| 811 |
+
}
|
| 812 |
+
|
| 813 |
+
// ============================================================================
|
| 814 |
+
|
| 815 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 816 |
+
namespace detail {
|
| 817 |
+
inline std::tuple<Tensor, Tensor> fractional_max_pool2d_with_indices(
|
| 818 |
+
const Tensor& input,
|
| 819 |
+
const ExpandingArray<2>& kernel_size,
|
| 820 |
+
const c10::optional<ExpandingArray<2>>& output_size,
|
| 821 |
+
const c10::optional<ExpandingArray<2, double>>& output_ratio,
|
| 822 |
+
const Tensor& _random_samples) {
|
| 823 |
+
if (output_size == c10::nullopt && output_ratio == c10::nullopt) {
|
| 824 |
+
TORCH_CHECK(
|
| 825 |
+
false,
|
| 826 |
+
"fractional_max_pool2d requires specifying either ",
|
| 827 |
+
"an output_size or an output_ratio");
|
| 828 |
+
}
|
| 829 |
+
c10::optional<ExpandingArray<2>> output_size_ = output_size;
|
| 830 |
+
if (output_size_ == c10::nullopt) {
|
| 831 |
+
TORCH_INTERNAL_ASSERT(output_ratio != c10::nullopt);
|
| 832 |
+
output_size_ = {
|
| 833 |
+
(int64_t)(static_cast<double>(input.size(-2)) * (*output_ratio.value())[0]),
|
| 834 |
+
(int64_t)(static_cast<double>(input.size(-1)) * (*output_ratio.value())[1])};
|
| 835 |
+
}
|
| 836 |
+
|
| 837 |
+
Tensor _random_samples_ = _random_samples;
|
| 838 |
+
if (!_random_samples_.defined()) {
|
| 839 |
+
auto n_batch = input.dim() == 3 ? 1 : input.size(0);
|
| 840 |
+
_random_samples_ = torch::rand(
|
| 841 |
+
{n_batch, input.size(-3), 2},
|
| 842 |
+
torch::TensorOptions().dtype(input.dtype()).device(input.device()));
|
| 843 |
+
}
|
| 844 |
+
return torch::fractional_max_pool2d(
|
| 845 |
+
input, kernel_size, *output_size_, _random_samples_);
|
| 846 |
+
}
|
| 847 |
+
} // namespace detail
|
| 848 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 849 |
+
|
| 850 |
+
/// See the documentation for
|
| 851 |
+
/// `torch::nn::functional::FractionalMaxPool2dFuncOptions` class to learn what
|
| 852 |
+
/// optional arguments are supported for this functional.
|
| 853 |
+
///
|
| 854 |
+
/// Example:
|
| 855 |
+
/// ```
|
| 856 |
+
/// namespace F = torch::nn::functional;
|
| 857 |
+
/// F::fractional_max_pool2d_with_indices(x,
|
| 858 |
+
/// F::FractionalMaxPool2dFuncOptions(3).output_size(2));
|
| 859 |
+
/// ```
|
| 860 |
+
inline std::tuple<Tensor, Tensor> fractional_max_pool2d_with_indices(
|
| 861 |
+
const Tensor& input,
|
| 862 |
+
const FractionalMaxPool2dFuncOptions& options) {
|
| 863 |
+
return detail::fractional_max_pool2d_with_indices(
|
| 864 |
+
input,
|
| 865 |
+
options.kernel_size(),
|
| 866 |
+
options.output_size(),
|
| 867 |
+
options.output_ratio(),
|
| 868 |
+
options._random_samples());
|
| 869 |
+
}
|
| 870 |
+
|
| 871 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 872 |
+
namespace detail {
|
| 873 |
+
inline Tensor fractional_max_pool2d(
|
| 874 |
+
const Tensor& input,
|
| 875 |
+
ExpandingArray<2> kernel_size,
|
| 876 |
+
c10::optional<ExpandingArray<2>> output_size,
|
| 877 |
+
c10::optional<ExpandingArray<2, double>> output_ratio,
|
| 878 |
+
const Tensor& _random_samples) {
|
| 879 |
+
return std::get<0>(fractional_max_pool2d_with_indices(
|
| 880 |
+
input, kernel_size, output_size, output_ratio, _random_samples));
|
| 881 |
+
}
|
| 882 |
+
} // namespace detail
|
| 883 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 884 |
+
|
| 885 |
+
/// See the documentation for
|
| 886 |
+
/// `torch::nn::functional::FractionalMaxPool2dFuncOptions` class to learn what
|
| 887 |
+
/// optional arguments are supported for this functional.
|
| 888 |
+
///
|
| 889 |
+
/// Example:
|
| 890 |
+
/// ```
|
| 891 |
+
/// namespace F = torch::nn::functional;
|
| 892 |
+
/// F::fractional_max_pool2d(x,
|
| 893 |
+
/// F::FractionalMaxPool2dFuncOptions(3).output_size(2));
|
| 894 |
+
/// ```
|
| 895 |
+
inline Tensor fractional_max_pool2d(
|
| 896 |
+
const Tensor& input,
|
| 897 |
+
const FractionalMaxPool2dFuncOptions& options) {
|
| 898 |
+
return detail::fractional_max_pool2d(
|
| 899 |
+
input,
|
| 900 |
+
options.kernel_size(),
|
| 901 |
+
options.output_size(),
|
| 902 |
+
options.output_ratio(),
|
| 903 |
+
options._random_samples());
|
| 904 |
+
}
|
| 905 |
+
|
| 906 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 907 |
+
namespace detail {
|
| 908 |
+
inline std::tuple<Tensor, Tensor> fractional_max_pool3d_with_indices(
|
| 909 |
+
const Tensor& input,
|
| 910 |
+
const ExpandingArray<3>& kernel_size,
|
| 911 |
+
const c10::optional<ExpandingArray<3>>& output_size,
|
| 912 |
+
const c10::optional<ExpandingArray<3, double>>& output_ratio,
|
| 913 |
+
const Tensor& _random_samples) {
|
| 914 |
+
if (output_size == c10::nullopt && output_ratio == c10::nullopt) {
|
| 915 |
+
TORCH_CHECK(
|
| 916 |
+
false,
|
| 917 |
+
"fractional_max_pool3d requires specifying either ",
|
| 918 |
+
"an output_size or an output_ratio");
|
| 919 |
+
}
|
| 920 |
+
|
| 921 |
+
c10::optional<ExpandingArray<3>> output_size_ = output_size;
|
| 922 |
+
if (output_size_ == c10::nullopt) {
|
| 923 |
+
TORCH_INTERNAL_ASSERT(output_ratio != c10::nullopt);
|
| 924 |
+
output_size_ = {
|
| 925 |
+
(int64_t)(static_cast<double>(input.size(-3)) * (*output_ratio.value())[0]),
|
| 926 |
+
(int64_t)(static_cast<double>(input.size(-2)) * (*output_ratio.value())[1]),
|
| 927 |
+
(int64_t)(static_cast<double>(input.size(-1)) * (*output_ratio.value())[2])};
|
| 928 |
+
}
|
| 929 |
+
|
| 930 |
+
Tensor _random_samples_ = _random_samples;
|
| 931 |
+
if (!_random_samples_.defined()) {
|
| 932 |
+
auto n_batch = input.dim() == 4 ? 1 : input.size(0);
|
| 933 |
+
_random_samples_ = torch::rand(
|
| 934 |
+
{n_batch, input.size(-4), 3},
|
| 935 |
+
torch::TensorOptions().dtype(input.dtype()).device(input.device()));
|
| 936 |
+
}
|
| 937 |
+
return torch::fractional_max_pool3d(
|
| 938 |
+
input, kernel_size, *output_size_, _random_samples_);
|
| 939 |
+
}
|
| 940 |
+
} // namespace detail
|
| 941 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 942 |
+
|
| 943 |
+
/// See the documentation for
|
| 944 |
+
/// `torch::nn::functional::FractionalMaxPool3dFuncOptions` class to learn what
|
| 945 |
+
/// optional arguments are supported for this functional.
|
| 946 |
+
///
|
| 947 |
+
/// Example:
|
| 948 |
+
/// ```
|
| 949 |
+
/// namespace F = torch::nn::functional;
|
| 950 |
+
/// F::fractional_max_pool3d_with_indices(x,
|
| 951 |
+
/// F::FractionalMaxPool3dFuncOptions(3).output_size(2));
|
| 952 |
+
/// ```
|
| 953 |
+
inline std::tuple<Tensor, Tensor> fractional_max_pool3d_with_indices(
|
| 954 |
+
const Tensor& input,
|
| 955 |
+
const FractionalMaxPool3dFuncOptions& options) {
|
| 956 |
+
return detail::fractional_max_pool3d_with_indices(
|
| 957 |
+
input,
|
| 958 |
+
options.kernel_size(),
|
| 959 |
+
options.output_size(),
|
| 960 |
+
options.output_ratio(),
|
| 961 |
+
options._random_samples());
|
| 962 |
+
}
|
| 963 |
+
|
| 964 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 965 |
+
namespace detail {
|
| 966 |
+
inline Tensor fractional_max_pool3d(
|
| 967 |
+
const Tensor& input,
|
| 968 |
+
ExpandingArray<3> kernel_size,
|
| 969 |
+
c10::optional<ExpandingArray<3>> output_size,
|
| 970 |
+
c10::optional<ExpandingArray<3, double>> output_ratio,
|
| 971 |
+
const Tensor& _random_samples) {
|
| 972 |
+
return std::get<0>(fractional_max_pool3d_with_indices(
|
| 973 |
+
input, kernel_size, output_size, output_ratio, _random_samples));
|
| 974 |
+
}
|
| 975 |
+
} // namespace detail
|
| 976 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 977 |
+
|
| 978 |
+
/// See the documentation for
|
| 979 |
+
/// `torch::nn::functional::FractionalMaxPool3dFuncOptions` class to learn what
|
| 980 |
+
/// optional arguments are supported for this functional.
|
| 981 |
+
///
|
| 982 |
+
/// Example:
|
| 983 |
+
/// ```
|
| 984 |
+
/// namespace F = torch::nn::functional;
|
| 985 |
+
/// F::fractional_max_pool3d(x,
|
| 986 |
+
/// F::FractionalMaxPool3dFuncOptions(3).output_size(2));
|
| 987 |
+
/// ```
|
| 988 |
+
inline Tensor fractional_max_pool3d(
|
| 989 |
+
const Tensor& input,
|
| 990 |
+
const FractionalMaxPool3dFuncOptions& options) {
|
| 991 |
+
return detail::fractional_max_pool3d(
|
| 992 |
+
input,
|
| 993 |
+
options.kernel_size(),
|
| 994 |
+
options.output_size(),
|
| 995 |
+
options.output_ratio(),
|
| 996 |
+
options._random_samples());
|
| 997 |
+
}
|
| 998 |
+
|
| 999 |
+
// ============================================================================
|
| 1000 |
+
|
| 1001 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 1002 |
+
namespace detail {
|
| 1003 |
+
inline Tensor lp_pool1d(
|
| 1004 |
+
const Tensor& input,
|
| 1005 |
+
double norm_type,
|
| 1006 |
+
ExpandingArray<1> kernel_size,
|
| 1007 |
+
ExpandingArray<1> stride,
|
| 1008 |
+
bool ceil_mode) {
|
| 1009 |
+
Tensor out = detail::avg_pool1d(
|
| 1010 |
+
input.pow(norm_type),
|
| 1011 |
+
kernel_size,
|
| 1012 |
+
stride,
|
| 1013 |
+
/*padding=*/0,
|
| 1014 |
+
ceil_mode,
|
| 1015 |
+
/*count_include_pad=*/true);
|
| 1016 |
+
|
| 1017 |
+
return (torch::sign(out) * relu(torch::abs(out)))
|
| 1018 |
+
.mul((*kernel_size)[0])
|
| 1019 |
+
.pow(1. / norm_type);
|
| 1020 |
+
}
|
| 1021 |
+
} // namespace detail
|
| 1022 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 1023 |
+
|
| 1024 |
+
/// See
|
| 1025 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.lp_pool1d
|
| 1026 |
+
/// about the exact behavior of this functional.
|
| 1027 |
+
///
|
| 1028 |
+
/// See the documentation for `torch::nn::functional::LPPool1dFuncOptions` class
|
| 1029 |
+
/// to learn what optional arguments are supported for this functional.
|
| 1030 |
+
///
|
| 1031 |
+
/// Example:
|
| 1032 |
+
/// ```
|
| 1033 |
+
/// namespace F = torch::nn::functional;
|
| 1034 |
+
/// F::lp_pool1d(x, F::LPPool1dFuncOptions(2, 3).stride(2));
|
| 1035 |
+
/// ```
|
| 1036 |
+
inline Tensor lp_pool1d(
|
| 1037 |
+
const Tensor& input,
|
| 1038 |
+
const LPPool1dFuncOptions& options) {
|
| 1039 |
+
return detail::lp_pool1d(
|
| 1040 |
+
input,
|
| 1041 |
+
options.norm_type(),
|
| 1042 |
+
options.kernel_size(),
|
| 1043 |
+
options.stride(),
|
| 1044 |
+
options.ceil_mode());
|
| 1045 |
+
}
|
| 1046 |
+
|
| 1047 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 1048 |
+
namespace detail {
|
| 1049 |
+
inline Tensor lp_pool2d(
|
| 1050 |
+
const Tensor& input,
|
| 1051 |
+
double norm_type,
|
| 1052 |
+
ExpandingArray<2> kernel_size,
|
| 1053 |
+
ExpandingArray<2> stride,
|
| 1054 |
+
bool ceil_mode) {
|
| 1055 |
+
int kw = (*kernel_size)[0];
|
| 1056 |
+
int kh = (*kernel_size)[1];
|
| 1057 |
+
Tensor out = detail::avg_pool2d(
|
| 1058 |
+
input.pow(norm_type),
|
| 1059 |
+
kernel_size,
|
| 1060 |
+
stride,
|
| 1061 |
+
/*padding=*/0,
|
| 1062 |
+
ceil_mode,
|
| 1063 |
+
/*count_include_pad=*/true,
|
| 1064 |
+
/*divisor_override=*/c10::nullopt);
|
| 1065 |
+
|
| 1066 |
+
return (torch::sign(out) * relu(torch::abs(out)))
|
| 1067 |
+
.mul(kw * kh)
|
| 1068 |
+
.pow(1. / norm_type);
|
| 1069 |
+
}
|
| 1070 |
+
} // namespace detail
|
| 1071 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 1072 |
+
|
| 1073 |
+
/// See
|
| 1074 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.lp_pool2d
|
| 1075 |
+
/// about the exact behavior of this functional.
|
| 1076 |
+
///
|
| 1077 |
+
/// See the documentation for `torch::nn::functional::LPPool2dFuncOptions` class
|
| 1078 |
+
/// to learn what optional arguments are supported for this functional.
|
| 1079 |
+
///
|
| 1080 |
+
/// Example:
|
| 1081 |
+
/// ```
|
| 1082 |
+
/// namespace F = torch::nn::functional;
|
| 1083 |
+
/// F::lp_pool2d(x, F::LPPool2dFuncOptions(2, {2, 3}).stride(2));
|
| 1084 |
+
/// ```
|
| 1085 |
+
inline Tensor lp_pool2d(
|
| 1086 |
+
const Tensor& input,
|
| 1087 |
+
const LPPool2dFuncOptions& options) {
|
| 1088 |
+
return detail::lp_pool2d(
|
| 1089 |
+
input,
|
| 1090 |
+
options.norm_type(),
|
| 1091 |
+
options.kernel_size(),
|
| 1092 |
+
options.stride(),
|
| 1093 |
+
options.ceil_mode());
|
| 1094 |
+
}
|
| 1095 |
+
|
| 1096 |
+
} // namespace functional
|
| 1097 |
+
} // namespace nn
|
| 1098 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/upsampling.h
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <c10/util/irange.h>
|
| 4 |
+
#include <torch/nn/functional/pooling.h>
|
| 5 |
+
#include <torch/nn/options/upsampling.h>
|
| 6 |
+
|
| 7 |
+
#include <cmath>
|
| 8 |
+
#include <utility>
|
| 9 |
+
|
| 10 |
+
namespace torch {
|
| 11 |
+
namespace nn {
|
| 12 |
+
namespace functional {
|
| 13 |
+
|
| 14 |
+
inline std::vector<int64_t> _interp_output_size(
|
| 15 |
+
int64_t dim,
|
| 16 |
+
std::tuple<
|
| 17 |
+
Tensor,
|
| 18 |
+
c10::optional<std::vector<int64_t>>,
|
| 19 |
+
c10::optional<std::vector<double>>,
|
| 20 |
+
c10::optional<bool>> closed_over_args) {
|
| 21 |
+
Tensor input;
|
| 22 |
+
c10::optional<std::vector<int64_t>> size;
|
| 23 |
+
c10::optional<std::vector<double>> scale_factor;
|
| 24 |
+
c10::optional<bool> recompute_scale_factor;
|
| 25 |
+
std::tie(input, size, scale_factor, recompute_scale_factor) =
|
| 26 |
+
closed_over_args;
|
| 27 |
+
if (size == c10::nullopt && scale_factor == c10::nullopt) {
|
| 28 |
+
TORCH_CHECK(false, "either size or scale_factor should be defined");
|
| 29 |
+
}
|
| 30 |
+
if (size != c10::nullopt && scale_factor != c10::nullopt) {
|
| 31 |
+
TORCH_CHECK(false, "only one of size or scale_factor should be defined");
|
| 32 |
+
}
|
| 33 |
+
if (scale_factor != c10::nullopt) {
|
| 34 |
+
if (static_cast<int64_t>(scale_factor.value().size()) != dim) {
|
| 35 |
+
TORCH_CHECK(
|
| 36 |
+
false,
|
| 37 |
+
"scale_factor shape must match input shape. ",
|
| 38 |
+
"Input is ",
|
| 39 |
+
dim,
|
| 40 |
+
"D, scale_factor size is ",
|
| 41 |
+
torch::ArrayRef<double>(*scale_factor));
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
if (size != c10::nullopt) {
|
| 45 |
+
return *size;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
TORCH_INTERNAL_ASSERT(scale_factor != c10::nullopt);
|
| 49 |
+
auto scale_factors = *scale_factor;
|
| 50 |
+
|
| 51 |
+
if (recompute_scale_factor == c10::nullopt) {
|
| 52 |
+
// only warn when the scales have floating values since
|
| 53 |
+
// the result for ints is the same with/without recompute_scale_factor
|
| 54 |
+
bool is_float_scale_factor = false;
|
| 55 |
+
for (double scale : scale_factors) {
|
| 56 |
+
is_float_scale_factor = floor(scale) != scale;
|
| 57 |
+
if (is_float_scale_factor) {
|
| 58 |
+
break;
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
if (is_float_scale_factor) {
|
| 62 |
+
TORCH_WARN(
|
| 63 |
+
"The default behavior for interpolate/upsample with float scale_factor changed "
|
| 64 |
+
"in 1.6.0 to align with other frameworks/libraries, and uses scale_factor directly, "
|
| 65 |
+
"instead of relying on the computed output size. "
|
| 66 |
+
"If you wish to keep the old behavior, please set recompute_scale_factor=True. "
|
| 67 |
+
"See the documentation of nn.Upsample for details. ");
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
std::vector<int64_t> ret;
|
| 72 |
+
for (const auto i : c10::irange(dim)) {
|
| 73 |
+
ret.emplace_back(static_cast<int64_t>(
|
| 74 |
+
floor(static_cast<double>(input.size(i + 2)) * scale_factors[i])));
|
| 75 |
+
}
|
| 76 |
+
return ret;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 80 |
+
namespace detail {
|
| 81 |
+
inline Tensor interpolate(
|
| 82 |
+
const Tensor& input,
|
| 83 |
+
const c10::optional<std::vector<int64_t>>& size,
|
| 84 |
+
const c10::optional<std::vector<double>>& scale_factor,
|
| 85 |
+
InterpolateFuncOptions::mode_t mode,
|
| 86 |
+
c10::optional<bool> align_corners,
|
| 87 |
+
c10::optional<bool> recompute_scale_factor,
|
| 88 |
+
bool antialias) {
|
| 89 |
+
if (std::holds_alternative<enumtype::kNearest>(mode) ||
|
| 90 |
+
std::get_if<enumtype::kArea>(&mode)) {
|
| 91 |
+
if (align_corners != c10::nullopt) {
|
| 92 |
+
TORCH_CHECK(
|
| 93 |
+
false,
|
| 94 |
+
"align_corners option can only be set with the "
|
| 95 |
+
"interpolating modes: linear | bilinear | bicubic | trilinear");
|
| 96 |
+
}
|
| 97 |
+
} else {
|
| 98 |
+
if (align_corners == c10::nullopt) {
|
| 99 |
+
TORCH_WARN(
|
| 100 |
+
"Default upsampling behavior when mode=",
|
| 101 |
+
enumtype::get_enum_name(mode),
|
| 102 |
+
" is changed "
|
| 103 |
+
"to align_corners=False since 0.4.0. Please specify "
|
| 104 |
+
"align_corners=True if the old behavior is desired. "
|
| 105 |
+
"See the documentation of nn.Upsample for details.");
|
| 106 |
+
align_corners = false;
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
TORCH_CHECK(
|
| 111 |
+
input.dim() >= 3 && input.dim() <= 5,
|
| 112 |
+
"Input Error: Only 3D, 4D and 5D input Tensors supported "
|
| 113 |
+
"(got ",
|
| 114 |
+
input.dim(),
|
| 115 |
+
"D) for the modes: nearest | linear | bilinear | bicubic | trilinear "
|
| 116 |
+
"(got ",
|
| 117 |
+
enumtype::get_enum_name(mode),
|
| 118 |
+
")");
|
| 119 |
+
|
| 120 |
+
auto scale_factor_len = input.dim() - 2;
|
| 121 |
+
std::vector<c10::optional<double>> scale_factor_list(
|
| 122 |
+
scale_factor_len, c10::nullopt);
|
| 123 |
+
if (scale_factor != c10::nullopt && !recompute_scale_factor.value_or(false)) {
|
| 124 |
+
auto _scale_factor_repeated = *scale_factor;
|
| 125 |
+
scale_factor_list = {};
|
| 126 |
+
for (const auto& elem : _scale_factor_repeated) {
|
| 127 |
+
scale_factor_list.emplace_back(elem);
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
if (antialias &&
|
| 132 |
+
!(input.dim() == 4 &&
|
| 133 |
+
(std::get_if<enumtype::kBilinear>(&mode) ||
|
| 134 |
+
std::get_if<enumtype::kBicubic>(&mode)))) {
|
| 135 |
+
TORCH_CHECK(
|
| 136 |
+
false,
|
| 137 |
+
"Anti-alias option is only supported for bilinear and bicubic modes");
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
auto closed_over_args =
|
| 141 |
+
std::make_tuple(input, size, scale_factor, recompute_scale_factor);
|
| 142 |
+
if (input.dim() == 3 && std::get_if<enumtype::kNearest>(&mode)) {
|
| 143 |
+
return torch::upsample_nearest1d(
|
| 144 |
+
input,
|
| 145 |
+
_interp_output_size(1, std::move(closed_over_args)),
|
| 146 |
+
scale_factor_list.at(0));
|
| 147 |
+
} else if (input.dim() == 4 && std::get_if<enumtype::kNearest>(&mode)) {
|
| 148 |
+
return torch::upsample_nearest2d(
|
| 149 |
+
input,
|
| 150 |
+
_interp_output_size(2, std::move(closed_over_args)),
|
| 151 |
+
scale_factor_list.at(0),
|
| 152 |
+
scale_factor_list.at(1));
|
| 153 |
+
} else if (input.dim() == 5 && std::get_if<enumtype::kNearest>(&mode)) {
|
| 154 |
+
return torch::upsample_nearest3d(
|
| 155 |
+
input,
|
| 156 |
+
_interp_output_size(3, std::move(closed_over_args)),
|
| 157 |
+
scale_factor_list.at(0),
|
| 158 |
+
scale_factor_list.at(1),
|
| 159 |
+
scale_factor_list.at(2));
|
| 160 |
+
} else if (input.dim() == 3 && std::get_if<enumtype::kNearestExact>(&mode)) {
|
| 161 |
+
return torch::_upsample_nearest_exact1d(
|
| 162 |
+
input,
|
| 163 |
+
_interp_output_size(1, std::move(closed_over_args)),
|
| 164 |
+
scale_factor_list.at(0));
|
| 165 |
+
} else if (input.dim() == 4 && std::get_if<enumtype::kNearestExact>(&mode)) {
|
| 166 |
+
return torch::_upsample_nearest_exact2d(
|
| 167 |
+
input,
|
| 168 |
+
_interp_output_size(2, std::move(closed_over_args)),
|
| 169 |
+
scale_factor_list.at(0),
|
| 170 |
+
scale_factor_list.at(1));
|
| 171 |
+
} else if (input.dim() == 5 && std::get_if<enumtype::kNearestExact>(&mode)) {
|
| 172 |
+
return torch::_upsample_nearest_exact3d(
|
| 173 |
+
input,
|
| 174 |
+
_interp_output_size(3, std::move(closed_over_args)),
|
| 175 |
+
scale_factor_list.at(0),
|
| 176 |
+
scale_factor_list.at(1),
|
| 177 |
+
scale_factor_list.at(2));
|
| 178 |
+
} else if (input.dim() == 3 && std::get_if<enumtype::kArea>(&mode)) {
|
| 179 |
+
return detail::adaptive_avg_pool1d(
|
| 180 |
+
input, _interp_output_size(1, std::move(closed_over_args)));
|
| 181 |
+
} else if (input.dim() == 4 && std::get_if<enumtype::kArea>(&mode)) {
|
| 182 |
+
return detail::adaptive_avg_pool2d(
|
| 183 |
+
input, _interp_output_size(2, std::move(closed_over_args)));
|
| 184 |
+
} else if (input.dim() == 5 && std::get_if<enumtype::kArea>(&mode)) {
|
| 185 |
+
return detail::adaptive_avg_pool3d(
|
| 186 |
+
input, _interp_output_size(3, std::move(closed_over_args)));
|
| 187 |
+
} else if (input.dim() == 3 && std::get_if<enumtype::kLinear>(&mode)) {
|
| 188 |
+
TORCH_INTERNAL_ASSERT(align_corners != c10::nullopt);
|
| 189 |
+
return torch::upsample_linear1d(
|
| 190 |
+
input,
|
| 191 |
+
_interp_output_size(1, std::move(closed_over_args)),
|
| 192 |
+
*align_corners,
|
| 193 |
+
scale_factor_list.at(0));
|
| 194 |
+
} else if (input.dim() == 3 && std::get_if<enumtype::kBilinear>(&mode)) {
|
| 195 |
+
TORCH_CHECK(false, "Got 3D input, but bilinear mode needs 4D input");
|
| 196 |
+
} else if (input.dim() == 3 && std::get_if<enumtype::kTrilinear>(&mode)) {
|
| 197 |
+
TORCH_CHECK(false, "Got 3D input, but trilinear mode needs 5D input");
|
| 198 |
+
} else if (input.dim() == 4 && std::get_if<enumtype::kLinear>(&mode)) {
|
| 199 |
+
TORCH_CHECK(false, "Got 4D input, but linear mode needs 3D input");
|
| 200 |
+
} else if (input.dim() == 4 && std::get_if<enumtype::kBilinear>(&mode)) {
|
| 201 |
+
TORCH_INTERNAL_ASSERT(align_corners != c10::nullopt);
|
| 202 |
+
if (antialias) {
|
| 203 |
+
return torch::_upsample_bilinear2d_aa(
|
| 204 |
+
input,
|
| 205 |
+
_interp_output_size(2, std::move(closed_over_args)),
|
| 206 |
+
*align_corners,
|
| 207 |
+
scale_factor_list.at(0),
|
| 208 |
+
scale_factor_list.at(1));
|
| 209 |
+
}
|
| 210 |
+
return torch::upsample_bilinear2d(
|
| 211 |
+
input,
|
| 212 |
+
_interp_output_size(2, std::move(closed_over_args)),
|
| 213 |
+
*align_corners,
|
| 214 |
+
scale_factor_list.at(0),
|
| 215 |
+
scale_factor_list.at(1));
|
| 216 |
+
} else if (input.dim() == 4 && std::get_if<enumtype::kTrilinear>(&mode)) {
|
| 217 |
+
TORCH_CHECK(false, "Got 4D input, but trilinear mode needs 5D input");
|
| 218 |
+
} else if (input.dim() == 5 && std::get_if<enumtype::kLinear>(&mode)) {
|
| 219 |
+
TORCH_CHECK(false, "Got 5D input, but linear mode needs 3D input");
|
| 220 |
+
} else if (input.dim() == 5 && std::get_if<enumtype::kBilinear>(&mode)) {
|
| 221 |
+
TORCH_CHECK(false, "Got 5D input, but bilinear mode needs 4D input");
|
| 222 |
+
} else if (input.dim() == 5 && std::get_if<enumtype::kTrilinear>(&mode)) {
|
| 223 |
+
TORCH_INTERNAL_ASSERT(align_corners != c10::nullopt);
|
| 224 |
+
return torch::upsample_trilinear3d(
|
| 225 |
+
input,
|
| 226 |
+
_interp_output_size(3, std::move(closed_over_args)),
|
| 227 |
+
*align_corners,
|
| 228 |
+
scale_factor_list.at(0),
|
| 229 |
+
scale_factor_list.at(1),
|
| 230 |
+
scale_factor_list.at(2));
|
| 231 |
+
} else if (input.dim() == 4 && std::get_if<enumtype::kBicubic>(&mode)) {
|
| 232 |
+
TORCH_INTERNAL_ASSERT(align_corners != c10::nullopt);
|
| 233 |
+
if (antialias) {
|
| 234 |
+
return torch::_upsample_bicubic2d_aa(
|
| 235 |
+
input,
|
| 236 |
+
_interp_output_size(2, std::move(closed_over_args)),
|
| 237 |
+
*align_corners,
|
| 238 |
+
scale_factor_list.at(0),
|
| 239 |
+
scale_factor_list.at(1));
|
| 240 |
+
}
|
| 241 |
+
return torch::upsample_bicubic2d(
|
| 242 |
+
input,
|
| 243 |
+
_interp_output_size(2, std::move(closed_over_args)),
|
| 244 |
+
*align_corners,
|
| 245 |
+
scale_factor_list.at(0),
|
| 246 |
+
scale_factor_list.at(1));
|
| 247 |
+
} else {
|
| 248 |
+
TORCH_CHECK(
|
| 249 |
+
false,
|
| 250 |
+
"Input Error: Only 3D, 4D and 5D input Tensors supported "
|
| 251 |
+
"(got ",
|
| 252 |
+
input.dim(),
|
| 253 |
+
"D) for the modes: nearest | linear | bilinear | bicubic | trilinear "
|
| 254 |
+
"(got ",
|
| 255 |
+
enumtype::get_enum_name(mode),
|
| 256 |
+
")");
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
} // namespace detail
|
| 260 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 261 |
+
|
| 262 |
+
/// See
|
| 263 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.interpolate
|
| 264 |
+
/// about the exact behavior of this functional.
|
| 265 |
+
///
|
| 266 |
+
/// See the documentation for `torch::nn::functional::InterpolateFuncOptions`
|
| 267 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 268 |
+
///
|
| 269 |
+
/// Example:
|
| 270 |
+
/// ```
|
| 271 |
+
/// namespace F = torch::nn::functional;
|
| 272 |
+
/// F::interpolate(input,
|
| 273 |
+
/// F::InterpolateFuncOptions().size({4}).mode(torch::kNearest));
|
| 274 |
+
/// ```
|
| 275 |
+
inline Tensor interpolate(
|
| 276 |
+
const Tensor& input,
|
| 277 |
+
const InterpolateFuncOptions& options = {}) {
|
| 278 |
+
return detail::interpolate(
|
| 279 |
+
input,
|
| 280 |
+
options.size(),
|
| 281 |
+
options.scale_factor(),
|
| 282 |
+
options.mode(),
|
| 283 |
+
options.align_corners(),
|
| 284 |
+
options.recompute_scale_factor(),
|
| 285 |
+
options.antialias());
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
} // namespace functional
|
| 289 |
+
} // namespace nn
|
| 290 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/functional/vision.h
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/options/vision.h>
|
| 4 |
+
#include <torch/types.h>
|
| 5 |
+
|
| 6 |
+
namespace torch {
|
| 7 |
+
namespace nn {
|
| 8 |
+
namespace functional {
|
| 9 |
+
|
| 10 |
+
inline Tensor affine_grid(
|
| 11 |
+
const Tensor& theta,
|
| 12 |
+
const IntArrayRef& size,
|
| 13 |
+
bool align_corners = false) {
|
| 14 |
+
// enforce floating point dtype on theta
|
| 15 |
+
TORCH_CHECK(
|
| 16 |
+
theta.is_floating_point(),
|
| 17 |
+
"Expected theta to have floating point type, but got ",
|
| 18 |
+
theta.dtype());
|
| 19 |
+
|
| 20 |
+
// check that shapes and sizes match
|
| 21 |
+
if (size.size() == 4) {
|
| 22 |
+
TORCH_CHECK(
|
| 23 |
+
theta.dim() == 3 && theta.size(-2) == 2 && theta.size(-1) == 3,
|
| 24 |
+
"Expected a batch of 2D affine matrices of shape Nx2x3 for size ",
|
| 25 |
+
size,
|
| 26 |
+
". Got ",
|
| 27 |
+
theta.sizes(),
|
| 28 |
+
".");
|
| 29 |
+
} else if (size.size() == 5) {
|
| 30 |
+
TORCH_CHECK(
|
| 31 |
+
theta.dim() == 3 && theta.size(-2) == 3 && theta.size(-1) == 4,
|
| 32 |
+
"Expected a batch of 3D affine matrices of shape Nx3x4 for size ",
|
| 33 |
+
size,
|
| 34 |
+
". Got ",
|
| 35 |
+
theta.sizes(),
|
| 36 |
+
".");
|
| 37 |
+
} else {
|
| 38 |
+
TORCH_CHECK(
|
| 39 |
+
false,
|
| 40 |
+
"affine_grid only supports 4D and 5D sizes, ",
|
| 41 |
+
"for 2D and 3D affine transforms, respectively. ",
|
| 42 |
+
"Got size ",
|
| 43 |
+
size);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
if (*std::min_element(size.begin(), size.end()) <= 0) {
|
| 47 |
+
TORCH_CHECK(false, "Expected non-zero, positive output size. Got ", size);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
return torch::affine_grid_generator(theta, size, align_corners);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// ============================================================================
|
| 54 |
+
|
| 55 |
+
#ifndef DOXYGEN_SHOULD_SKIP_THIS
|
| 56 |
+
namespace detail {
|
| 57 |
+
inline Tensor grid_sample(
|
| 58 |
+
const Tensor& input,
|
| 59 |
+
const Tensor& grid,
|
| 60 |
+
GridSampleFuncOptions::mode_t mode,
|
| 61 |
+
GridSampleFuncOptions::padding_mode_t padding_mode,
|
| 62 |
+
c10::optional<bool> align_corners) {
|
| 63 |
+
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
| 64 |
+
int64_t mode_enum, padding_mode_enum;
|
| 65 |
+
|
| 66 |
+
if (std::holds_alternative<enumtype::kBilinear>(mode)) {
|
| 67 |
+
mode_enum = 0;
|
| 68 |
+
} else if (std::holds_alternative<enumtype::kNearest>(mode)) {
|
| 69 |
+
mode_enum = 1;
|
| 70 |
+
} else { /// mode == 'bicubic'
|
| 71 |
+
mode_enum = 2;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
if (std::holds_alternative<enumtype::kZeros>(padding_mode)) {
|
| 75 |
+
padding_mode_enum = 0;
|
| 76 |
+
} else if (std::holds_alternative<enumtype::kBorder>(padding_mode)) {
|
| 77 |
+
padding_mode_enum = 1;
|
| 78 |
+
} else { /// padding_mode == 'reflection'
|
| 79 |
+
padding_mode_enum = 2;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
if (!align_corners.has_value()) {
|
| 83 |
+
TORCH_WARN(
|
| 84 |
+
"Default grid_sample and affine_grid behavior has changed ",
|
| 85 |
+
"to align_corners=False since 1.3.0. Please specify ",
|
| 86 |
+
"align_corners=True if the old behavior is desired. ",
|
| 87 |
+
"See the documentation of grid_sample for details.");
|
| 88 |
+
align_corners = false;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
return torch::grid_sampler(
|
| 92 |
+
input, grid, mode_enum, padding_mode_enum, align_corners.value());
|
| 93 |
+
}
|
| 94 |
+
} // namespace detail
|
| 95 |
+
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
|
| 96 |
+
|
| 97 |
+
/// See
|
| 98 |
+
/// https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.grid_sample
|
| 99 |
+
/// about the exact behavior of this functional.
|
| 100 |
+
///
|
| 101 |
+
/// See the documentation for `torch::nn::functional::GridSampleFuncOptions`
|
| 102 |
+
/// class to learn what optional arguments are supported for this functional.
|
| 103 |
+
///
|
| 104 |
+
/// Example:
|
| 105 |
+
/// ```
|
| 106 |
+
/// namespace F = torch::nn::functional;
|
| 107 |
+
/// F::grid_sample(input, grid,
|
| 108 |
+
/// F::GridSampleFuncOptions().mode(torch::kBilinear).padding_mode(torch::kZeros).align_corners(true));
|
| 109 |
+
/// ```
|
| 110 |
+
inline Tensor grid_sample(
|
| 111 |
+
const Tensor& input,
|
| 112 |
+
const Tensor& grid,
|
| 113 |
+
const GridSampleFuncOptions& options = {}) {
|
| 114 |
+
return detail::grid_sample(
|
| 115 |
+
input,
|
| 116 |
+
grid,
|
| 117 |
+
options.mode(),
|
| 118 |
+
options.padding_mode(),
|
| 119 |
+
options.align_corners());
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
} // namespace functional
|
| 123 |
+
} // namespace nn
|
| 124 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/_functions.h
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/csrc/autograd/custom_function.h>
|
| 4 |
+
#include <torch/csrc/autograd/variable.h>
|
| 5 |
+
#include <torch/nn/options/normalization.h>
|
| 6 |
+
#include <torch/types.h>
|
| 7 |
+
|
| 8 |
+
namespace torch {
|
| 9 |
+
namespace nn {
|
| 10 |
+
namespace functions {
|
| 11 |
+
|
| 12 |
+
class CrossMapLRN2d : public torch::autograd::Function<CrossMapLRN2d> {
|
| 13 |
+
public:
|
| 14 |
+
static torch::autograd::Variable forward(
|
| 15 |
+
torch::autograd::AutogradContext* ctx,
|
| 16 |
+
const torch::autograd::Variable& input,
|
| 17 |
+
const CrossMapLRN2dOptions& options);
|
| 18 |
+
|
| 19 |
+
static torch::autograd::variable_list backward(
|
| 20 |
+
torch::autograd::AutogradContext* ctx,
|
| 21 |
+
torch::autograd::variable_list grad_output);
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
} // namespace functions
|
| 25 |
+
} // namespace nn
|
| 26 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/activation.h
ADDED
|
@@ -0,0 +1,875 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/cloneable.h>
|
| 4 |
+
#include <torch/nn/functional/activation.h>
|
| 5 |
+
#include <torch/nn/modules/common.h>
|
| 6 |
+
#include <torch/nn/modules/linear.h>
|
| 7 |
+
#include <torch/nn/options/activation.h>
|
| 8 |
+
|
| 9 |
+
#include <torch/csrc/Export.h>
|
| 10 |
+
|
| 11 |
+
namespace torch {
|
| 12 |
+
namespace nn {
|
| 13 |
+
|
| 14 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ELU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 15 |
+
|
| 16 |
+
/// Applies elu over a given input.
|
| 17 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.ELU to learn
|
| 18 |
+
/// about the exact behavior of this module.
|
| 19 |
+
///
|
| 20 |
+
/// See the documentation for `torch::nn::ELUOptions` class to learn what
|
| 21 |
+
/// constructor arguments are supported for this module.
|
| 22 |
+
///
|
| 23 |
+
/// Example:
|
| 24 |
+
/// ```
|
| 25 |
+
/// ELU model(ELUOptions().alpha(42.42).inplace(true));
|
| 26 |
+
/// ```
|
| 27 |
+
class TORCH_API ELUImpl : public torch::nn::Cloneable<ELUImpl> {
|
| 28 |
+
public:
|
| 29 |
+
explicit ELUImpl(const ELUOptions& options_ = {});
|
| 30 |
+
|
| 31 |
+
Tensor forward(Tensor input);
|
| 32 |
+
|
| 33 |
+
void reset() override;
|
| 34 |
+
|
| 35 |
+
/// Pretty prints the `ELU` module into the given `stream`.
|
| 36 |
+
void pretty_print(std::ostream& stream) const override;
|
| 37 |
+
|
| 38 |
+
/// The options with which this `Module` was constructed.
|
| 39 |
+
ELUOptions options;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
/// A `ModuleHolder` subclass for `ELUImpl`.
|
| 43 |
+
/// See the documentation for `ELUImpl` class to learn what methods it
|
| 44 |
+
/// provides, and examples of how to use `ELU` with `torch::nn::ELUOptions`.
|
| 45 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 46 |
+
/// module storage semantics.
|
| 47 |
+
TORCH_MODULE(ELU);
|
| 48 |
+
|
| 49 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SELU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 50 |
+
|
| 51 |
+
/// Applies the selu function element-wise.
|
| 52 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.SELU to learn
|
| 53 |
+
/// about the exact behavior of this module.
|
| 54 |
+
///
|
| 55 |
+
/// See the documentation for `torch::nn::SELUOptions` class to learn what
|
| 56 |
+
/// constructor arguments are supported for this module.
|
| 57 |
+
///
|
| 58 |
+
/// Example:
|
| 59 |
+
/// ```
|
| 60 |
+
/// SELU model(SELUOptions().inplace(true));
|
| 61 |
+
/// ```
|
| 62 |
+
class TORCH_API SELUImpl : public torch::nn::Cloneable<SELUImpl> {
|
| 63 |
+
public:
|
| 64 |
+
explicit SELUImpl(const SELUOptions& options_ = {});
|
| 65 |
+
|
| 66 |
+
Tensor forward(Tensor input);
|
| 67 |
+
|
| 68 |
+
void reset() override;
|
| 69 |
+
|
| 70 |
+
/// Pretty prints the `SELU` module into the given `stream`.
|
| 71 |
+
void pretty_print(std::ostream& stream) const override;
|
| 72 |
+
|
| 73 |
+
/// The options with which this `Module` was constructed.
|
| 74 |
+
SELUOptions options;
|
| 75 |
+
};
|
| 76 |
+
|
| 77 |
+
/// A `ModuleHolder` subclass for `SELUImpl`.
|
| 78 |
+
/// See the documentation for `SELUImpl` class to learn what methods it
|
| 79 |
+
/// provides, and examples of how to use `SELU` with `torch::nn::SELUOptions`.
|
| 80 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 81 |
+
/// module storage semantics.
|
| 82 |
+
TORCH_MODULE(SELU);
|
| 83 |
+
|
| 84 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hardshrink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 85 |
+
|
| 86 |
+
/// Applies the hard shrinkage function element-wise.
|
| 87 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Hardshrink to learn
|
| 88 |
+
/// about the exact behavior of this module.
|
| 89 |
+
///
|
| 90 |
+
/// See the documentation for `torch::nn::HardshrinkOptions` class to learn what
|
| 91 |
+
/// constructor arguments are supported for this module.
|
| 92 |
+
///
|
| 93 |
+
/// Example:
|
| 94 |
+
/// ```
|
| 95 |
+
/// Hardshrink model(HardshrinkOptions().lambda(42.42));
|
| 96 |
+
/// ```
|
| 97 |
+
class TORCH_API HardshrinkImpl : public torch::nn::Cloneable<HardshrinkImpl> {
|
| 98 |
+
public:
|
| 99 |
+
explicit HardshrinkImpl(const HardshrinkOptions& options_ = {});
|
| 100 |
+
|
| 101 |
+
Tensor forward(const Tensor& input);
|
| 102 |
+
|
| 103 |
+
void reset() override;
|
| 104 |
+
|
| 105 |
+
/// Pretty prints the `Hardshrink` module into the given `stream`.
|
| 106 |
+
void pretty_print(std::ostream& stream) const override;
|
| 107 |
+
|
| 108 |
+
/// The options with which this `Module` was constructed.
|
| 109 |
+
HardshrinkOptions options;
|
| 110 |
+
};
|
| 111 |
+
|
| 112 |
+
/// A `ModuleHolder` subclass for `HardshrinkImpl`.
|
| 113 |
+
/// See the documentation for `HardshrinkImpl` class to learn what methods it
|
| 114 |
+
/// provides, and examples of how to use `Hardshrink` with
|
| 115 |
+
/// `torch::nn::HardshrinkOptions`. See the documentation for `ModuleHolder` to
|
| 116 |
+
/// learn about PyTorch's module storage semantics.
|
| 117 |
+
TORCH_MODULE(Hardshrink);
|
| 118 |
+
|
| 119 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hardtanh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 120 |
+
|
| 121 |
+
/// Applies the HardTanh function element-wise.
|
| 122 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Hardtanh to learn
|
| 123 |
+
/// about the exact behavior of this module.
|
| 124 |
+
///
|
| 125 |
+
/// See the documentation for `torch::nn::HardtanhOptions` class to learn what
|
| 126 |
+
/// constructor arguments are supported for this module.
|
| 127 |
+
///
|
| 128 |
+
/// Example:
|
| 129 |
+
/// ```
|
| 130 |
+
/// Hardtanh
|
| 131 |
+
/// model(HardtanhOptions().min_val(-42.42).max_val(0.42).inplace(true));
|
| 132 |
+
/// ```
|
| 133 |
+
class TORCH_API HardtanhImpl : public torch::nn::Cloneable<HardtanhImpl> {
|
| 134 |
+
public:
|
| 135 |
+
explicit HardtanhImpl(const HardtanhOptions& options_ = {});
|
| 136 |
+
|
| 137 |
+
Tensor forward(Tensor input);
|
| 138 |
+
|
| 139 |
+
void reset() override;
|
| 140 |
+
|
| 141 |
+
/// Pretty prints the `Hardtanh` module into the given `stream`.
|
| 142 |
+
void pretty_print(std::ostream& stream) const override;
|
| 143 |
+
|
| 144 |
+
/// The options with which this `Module` was constructed.
|
| 145 |
+
HardtanhOptions options;
|
| 146 |
+
};
|
| 147 |
+
|
| 148 |
+
/// A `ModuleHolder` subclass for `HardtanhImpl`.
|
| 149 |
+
/// See the documentation for `HardtanhImpl` class to learn what methods it
|
| 150 |
+
/// provides, and examples of how to use `Hardtanh` with
|
| 151 |
+
/// `torch::nn::HardtanhOptions`. See the documentation for `ModuleHolder` to
|
| 152 |
+
/// learn about PyTorch's module storage semantics.
|
| 153 |
+
TORCH_MODULE(Hardtanh);
|
| 154 |
+
|
| 155 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LeakyReLU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 156 |
+
|
| 157 |
+
/// Applies the LeakyReLU function element-wise.
|
| 158 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.LeakyReLU to learn
|
| 159 |
+
/// about the exact behavior of this module.
|
| 160 |
+
///
|
| 161 |
+
/// See the documentation for `torch::nn::LeakyReLUOptions` class to learn what
|
| 162 |
+
/// constructor arguments are supported for this module.
|
| 163 |
+
///
|
| 164 |
+
/// Example:
|
| 165 |
+
/// ```
|
| 166 |
+
/// LeakyReLU model(LeakyReLUOptions().negative_slope(0.42).inplace(true));
|
| 167 |
+
/// ```
|
| 168 |
+
class TORCH_API LeakyReLUImpl : public torch::nn::Cloneable<LeakyReLUImpl> {
|
| 169 |
+
public:
|
| 170 |
+
explicit LeakyReLUImpl(const LeakyReLUOptions& options_ = {});
|
| 171 |
+
|
| 172 |
+
Tensor forward(Tensor input);
|
| 173 |
+
|
| 174 |
+
void reset() override;
|
| 175 |
+
|
| 176 |
+
/// Pretty prints the `LeakyReLU` module into the given `stream`.
|
| 177 |
+
void pretty_print(std::ostream& stream) const override;
|
| 178 |
+
|
| 179 |
+
/// The options with which this `Module` was constructed.
|
| 180 |
+
LeakyReLUOptions options;
|
| 181 |
+
};
|
| 182 |
+
|
| 183 |
+
/// A `ModuleHolder` subclass for `LeakyReLUImpl`.
|
| 184 |
+
/// See the documentation for `LeakyReLUImpl` class to learn what methods it
|
| 185 |
+
/// provides, and examples of how to use `LeakyReLU` with
|
| 186 |
+
/// `torch::nn::LeakyReLUOptions`. See the documentation for `ModuleHolder` to
|
| 187 |
+
/// learn about PyTorch's module storage semantics.
|
| 188 |
+
TORCH_MODULE(LeakyReLU);
|
| 189 |
+
|
| 190 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LogSigmoid ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 191 |
+
|
| 192 |
+
/// Applies the LogSigmoid function element-wise.
|
| 193 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.LogSigmoid to learn
|
| 194 |
+
/// about the exact behavior of this module.
|
| 195 |
+
class TORCH_API LogSigmoidImpl : public torch::nn::Cloneable<LogSigmoidImpl> {
|
| 196 |
+
public:
|
| 197 |
+
Tensor forward(const Tensor& input);
|
| 198 |
+
|
| 199 |
+
void reset() override;
|
| 200 |
+
|
| 201 |
+
/// Pretty prints the `LogSigmoid` module into the given `stream`.
|
| 202 |
+
void pretty_print(std::ostream& stream) const override;
|
| 203 |
+
};
|
| 204 |
+
|
| 205 |
+
/// A `ModuleHolder` subclass for `LogSigmoidImpl`.
|
| 206 |
+
/// See the documentation for `LogSigmoidImpl` class to learn what methods it
|
| 207 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 208 |
+
/// module storage semantics.
|
| 209 |
+
TORCH_MODULE(LogSigmoid);
|
| 210 |
+
|
| 211 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Softmax ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 212 |
+
|
| 213 |
+
/// Applies the Softmax function.
|
| 214 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Softmax to learn
|
| 215 |
+
/// about the exact behavior of this module.
|
| 216 |
+
///
|
| 217 |
+
/// See the documentation for `torch::nn::SoftmaxOptions` class to learn what
|
| 218 |
+
/// constructor arguments are supported for this module.
|
| 219 |
+
///
|
| 220 |
+
/// Example:
|
| 221 |
+
/// ```
|
| 222 |
+
/// Softmax model(SoftmaxOptions(1));
|
| 223 |
+
/// ```
|
| 224 |
+
class TORCH_API SoftmaxImpl : public torch::nn::Cloneable<SoftmaxImpl> {
|
| 225 |
+
public:
|
| 226 |
+
explicit SoftmaxImpl(int64_t dim) : SoftmaxImpl(SoftmaxOptions(dim)) {}
|
| 227 |
+
explicit SoftmaxImpl(const SoftmaxOptions& options_);
|
| 228 |
+
|
| 229 |
+
Tensor forward(const Tensor& input);
|
| 230 |
+
|
| 231 |
+
void reset() override;
|
| 232 |
+
|
| 233 |
+
/// Pretty prints the `Softmax` module into the given `stream`.
|
| 234 |
+
void pretty_print(std::ostream& stream) const override;
|
| 235 |
+
|
| 236 |
+
SoftmaxOptions options;
|
| 237 |
+
};
|
| 238 |
+
|
| 239 |
+
/// A `ModuleHolder` subclass for `SoftmaxImpl`.
|
| 240 |
+
/// See the documentation for `SoftmaxImpl` class to learn what methods it
|
| 241 |
+
/// provides, and examples of how to use `Softmax` with
|
| 242 |
+
/// `torch::nn::SoftmaxOptions`. See the documentation for `ModuleHolder` to
|
| 243 |
+
/// learn about PyTorch's module storage semantics.
|
| 244 |
+
TORCH_MODULE(Softmax);
|
| 245 |
+
|
| 246 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Softmin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 247 |
+
|
| 248 |
+
/// Applies the Softmin function element-wise.
|
| 249 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Softmin to learn
|
| 250 |
+
/// about the exact behavior of this module.
|
| 251 |
+
///
|
| 252 |
+
/// See the documentation for `torch::nn::SoftminOptions` class to learn what
|
| 253 |
+
/// constructor arguments are supported for this module.
|
| 254 |
+
///
|
| 255 |
+
/// Example:
|
| 256 |
+
/// ```
|
| 257 |
+
/// Softmin model(SoftminOptions(1));
|
| 258 |
+
/// ```
|
| 259 |
+
class TORCH_API SoftminImpl : public torch::nn::Cloneable<SoftminImpl> {
|
| 260 |
+
public:
|
| 261 |
+
explicit SoftminImpl(int64_t dim) : SoftminImpl(SoftminOptions(dim)) {}
|
| 262 |
+
explicit SoftminImpl(const SoftminOptions& options_);
|
| 263 |
+
|
| 264 |
+
Tensor forward(const Tensor& input);
|
| 265 |
+
|
| 266 |
+
void reset() override;
|
| 267 |
+
|
| 268 |
+
/// Pretty prints the `Softmin` module into the given `stream`.
|
| 269 |
+
void pretty_print(std::ostream& stream) const override;
|
| 270 |
+
|
| 271 |
+
SoftminOptions options;
|
| 272 |
+
};
|
| 273 |
+
|
| 274 |
+
/// A `ModuleHolder` subclass for `SoftminImpl`.
|
| 275 |
+
/// See the documentation for `SoftminImpl` class to learn what methods it
|
| 276 |
+
/// provides, and examples of how to use `Softmin` with
|
| 277 |
+
/// `torch::nn::SoftminOptions`. See the documentation for `ModuleHolder` to
|
| 278 |
+
/// learn about PyTorch's module storage semantics.
|
| 279 |
+
TORCH_MODULE(Softmin);
|
| 280 |
+
|
| 281 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LogSoftmax ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 282 |
+
|
| 283 |
+
/// Applies the LogSoftmax function element-wise.
|
| 284 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.LogSoftmax to learn
|
| 285 |
+
/// about the exact behavior of this module.
|
| 286 |
+
///
|
| 287 |
+
/// See the documentation for `torch::nn::LogSoftmaxOptions` class to learn what
|
| 288 |
+
/// constructor arguments are supported for this module.
|
| 289 |
+
///
|
| 290 |
+
/// Example:
|
| 291 |
+
/// ```
|
| 292 |
+
/// LogSoftmax model(LogSoftmaxOptions(1));
|
| 293 |
+
/// ```
|
| 294 |
+
class TORCH_API LogSoftmaxImpl : public torch::nn::Cloneable<LogSoftmaxImpl> {
|
| 295 |
+
public:
|
| 296 |
+
explicit LogSoftmaxImpl(int64_t dim)
|
| 297 |
+
: LogSoftmaxImpl(LogSoftmaxOptions(dim)) {}
|
| 298 |
+
explicit LogSoftmaxImpl(const LogSoftmaxOptions& options_);
|
| 299 |
+
|
| 300 |
+
Tensor forward(const Tensor& input);
|
| 301 |
+
|
| 302 |
+
void reset() override;
|
| 303 |
+
|
| 304 |
+
/// Pretty prints the `LogSoftmax` module into the given `stream`.
|
| 305 |
+
void pretty_print(std::ostream& stream) const override;
|
| 306 |
+
|
| 307 |
+
LogSoftmaxOptions options;
|
| 308 |
+
};
|
| 309 |
+
|
| 310 |
+
/// A `ModuleHolder` subclass for `LogSoftmaxImpl`.
|
| 311 |
+
/// See the documentation for `LogSoftmaxImpl` class to learn what methods it
|
| 312 |
+
/// provides, and examples of how to use `LogSoftmax` with
|
| 313 |
+
/// `torch::nn::LogSoftmaxOptions`. See the documentation for `ModuleHolder` to
|
| 314 |
+
/// learn about PyTorch's module storage semantics.
|
| 315 |
+
TORCH_MODULE(LogSoftmax);
|
| 316 |
+
|
| 317 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Softmax2d ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 318 |
+
|
| 319 |
+
/// Applies the Softmax2d function element-wise.
|
| 320 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Softmax2d to learn
|
| 321 |
+
/// about the exact behavior of this module.
|
| 322 |
+
class TORCH_API Softmax2dImpl : public torch::nn::Cloneable<Softmax2dImpl> {
|
| 323 |
+
public:
|
| 324 |
+
Tensor forward(const Tensor& input);
|
| 325 |
+
|
| 326 |
+
void reset() override;
|
| 327 |
+
|
| 328 |
+
/// Pretty prints the `Softmax2d` module into the given `stream`.
|
| 329 |
+
void pretty_print(std::ostream& stream) const override;
|
| 330 |
+
};
|
| 331 |
+
|
| 332 |
+
/// A `ModuleHolder` subclass for `Softmax2dImpl`.
|
| 333 |
+
/// See the documentation for `Softmax2dImpl` class to learn what methods it
|
| 334 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 335 |
+
/// module storage semantics.
|
| 336 |
+
TORCH_MODULE(Softmax2d);
|
| 337 |
+
|
| 338 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PReLU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 339 |
+
|
| 340 |
+
/// Applies the PReLU function element-wise.
|
| 341 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.PReLU to learn
|
| 342 |
+
/// about the exact behavior of this module.
|
| 343 |
+
///
|
| 344 |
+
/// See the documentation for `torch::nn::PReLUOptions` class to learn what
|
| 345 |
+
/// constructor arguments are supported for this module.
|
| 346 |
+
///
|
| 347 |
+
/// Example:
|
| 348 |
+
/// ```
|
| 349 |
+
/// PReLU model(PReLUOptions().num_parameters(42));
|
| 350 |
+
/// ```
|
| 351 |
+
class TORCH_API PReLUImpl : public torch::nn::Cloneable<PReLUImpl> {
|
| 352 |
+
public:
|
| 353 |
+
explicit PReLUImpl(const PReLUOptions& options_ = {});
|
| 354 |
+
|
| 355 |
+
Tensor forward(const Tensor& input);
|
| 356 |
+
|
| 357 |
+
void reset() override;
|
| 358 |
+
|
| 359 |
+
/// Pretty prints the `PReLU` module into the given `stream`.
|
| 360 |
+
void pretty_print(std::ostream& stream) const override;
|
| 361 |
+
|
| 362 |
+
/// The options with which this `Module` was constructed.
|
| 363 |
+
PReLUOptions options;
|
| 364 |
+
|
| 365 |
+
/// The learned weight.
|
| 366 |
+
Tensor weight;
|
| 367 |
+
};
|
| 368 |
+
|
| 369 |
+
/// A `ModuleHolder` subclass for `PReLUImpl`.
|
| 370 |
+
/// See the documentation for `PReLUImpl` class to learn what methods it
|
| 371 |
+
/// provides, and examples of how to use `PReLU` with `torch::nn::PReLUOptions`.
|
| 372 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 373 |
+
/// module storage semantics.
|
| 374 |
+
TORCH_MODULE(PReLU);
|
| 375 |
+
|
| 376 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ReLU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 377 |
+
|
| 378 |
+
/// Applies the ReLU function element-wise.
|
| 379 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.ReLU to learn
|
| 380 |
+
/// about the exact behavior of this module.
|
| 381 |
+
///
|
| 382 |
+
/// See the documentation for `torch::nn::ReLUOptions` class to learn what
|
| 383 |
+
/// constructor arguments are supported for this module.
|
| 384 |
+
///
|
| 385 |
+
/// Example:
|
| 386 |
+
/// ```
|
| 387 |
+
/// ReLU model(ReLUOptions().inplace(true));
|
| 388 |
+
/// ```
|
| 389 |
+
class TORCH_API ReLUImpl : public torch::nn::Cloneable<ReLUImpl> {
|
| 390 |
+
public:
|
| 391 |
+
explicit ReLUImpl(const ReLUOptions& options_ = {});
|
| 392 |
+
|
| 393 |
+
Tensor forward(Tensor input);
|
| 394 |
+
|
| 395 |
+
void reset() override;
|
| 396 |
+
|
| 397 |
+
/// Pretty prints the `ReLU` module into the given `stream`.
|
| 398 |
+
void pretty_print(std::ostream& stream) const override;
|
| 399 |
+
|
| 400 |
+
/// The options with which this `Module` was constructed.
|
| 401 |
+
ReLUOptions options;
|
| 402 |
+
};
|
| 403 |
+
|
| 404 |
+
/// A `ModuleHolder` subclass for `ReLUImpl`.
|
| 405 |
+
/// See the documentation for `ReLUImpl` class to learn what methods it
|
| 406 |
+
/// provides, and examples of how to use `ReLU` with `torch::nn::ReLUOptions`.
|
| 407 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 408 |
+
/// module storage semantics.
|
| 409 |
+
TORCH_MODULE(ReLU);
|
| 410 |
+
|
| 411 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ReLU6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 412 |
+
|
| 413 |
+
/// Applies the ReLU6 function element-wise.
|
| 414 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.ReLU6 to learn
|
| 415 |
+
/// about the exact behavior of this module.
|
| 416 |
+
///
|
| 417 |
+
/// See the documentation for `torch::nn::ReLU6Options` class to learn what
|
| 418 |
+
/// constructor arguments are supported for this module.
|
| 419 |
+
///
|
| 420 |
+
/// Example:
|
| 421 |
+
/// ```
|
| 422 |
+
/// ReLU6 model(ReLU6Options().inplace(true));
|
| 423 |
+
/// ```
|
| 424 |
+
class TORCH_API ReLU6Impl : public torch::nn::Cloneable<ReLU6Impl> {
|
| 425 |
+
public:
|
| 426 |
+
explicit ReLU6Impl(const ReLU6Options& options_ = {});
|
| 427 |
+
|
| 428 |
+
Tensor forward(Tensor input);
|
| 429 |
+
|
| 430 |
+
void reset() override;
|
| 431 |
+
|
| 432 |
+
/// Pretty prints the `ReLU6` module into the given `stream`.
|
| 433 |
+
void pretty_print(std::ostream& stream) const override;
|
| 434 |
+
|
| 435 |
+
/// The options with which this `Module` was constructed.
|
| 436 |
+
ReLU6Options options;
|
| 437 |
+
};
|
| 438 |
+
|
| 439 |
+
/// A `ModuleHolder` subclass for `ReLU6Impl`.
|
| 440 |
+
/// See the documentation for `ReLU6Impl` class to learn what methods it
|
| 441 |
+
/// provides, and examples of how to use `ReLU6` with `torch::nn::ReLU6Options`.
|
| 442 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 443 |
+
/// module storage semantics.
|
| 444 |
+
TORCH_MODULE(ReLU6);
|
| 445 |
+
|
| 446 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RReLU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 447 |
+
|
| 448 |
+
/// Applies the RReLU function element-wise.
|
| 449 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.RReLU to learn
|
| 450 |
+
/// about the exact behavior of this module.
|
| 451 |
+
///
|
| 452 |
+
/// See the documentation for `torch::nn::RReLUOptions` class to learn what
|
| 453 |
+
/// constructor arguments are supported for this module.
|
| 454 |
+
///
|
| 455 |
+
/// Example:
|
| 456 |
+
/// ```
|
| 457 |
+
/// RReLU model(RReLUOptions().lower(0.24).upper(0.42).inplace(true));
|
| 458 |
+
/// ```
|
| 459 |
+
class TORCH_API RReLUImpl : public torch::nn::Cloneable<RReLUImpl> {
|
| 460 |
+
public:
|
| 461 |
+
explicit RReLUImpl(const RReLUOptions& options_ = {});
|
| 462 |
+
|
| 463 |
+
Tensor forward(Tensor input);
|
| 464 |
+
|
| 465 |
+
void reset() override;
|
| 466 |
+
|
| 467 |
+
/// Pretty prints the `RReLU` module into the given `stream`.
|
| 468 |
+
void pretty_print(std::ostream& stream) const override;
|
| 469 |
+
|
| 470 |
+
/// The options with which this `Module` was constructed.
|
| 471 |
+
RReLUOptions options;
|
| 472 |
+
};
|
| 473 |
+
|
| 474 |
+
/// A `ModuleHolder` subclass for `RReLUImpl`.
|
| 475 |
+
/// See the documentation for `RReLUImpl` class to learn what methods it
|
| 476 |
+
/// provides, and examples of how to use `RReLU` with `torch::nn::RReLUOptions`.
|
| 477 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 478 |
+
/// module storage semantics.
|
| 479 |
+
TORCH_MODULE(RReLU);
|
| 480 |
+
|
| 481 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CELU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 482 |
+
|
| 483 |
+
/// Applies celu over a given input.
|
| 484 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.CELU to learn
|
| 485 |
+
/// about the exact behavior of this module.
|
| 486 |
+
///
|
| 487 |
+
/// See the documentation for `torch::nn::CELUOptions` class to learn what
|
| 488 |
+
/// constructor arguments are supported for this module.
|
| 489 |
+
///
|
| 490 |
+
/// Example:
|
| 491 |
+
/// ```
|
| 492 |
+
/// CELU model(CELUOptions().alpha(42.42).inplace(true));
|
| 493 |
+
/// ```
|
| 494 |
+
class TORCH_API CELUImpl : public torch::nn::Cloneable<CELUImpl> {
|
| 495 |
+
public:
|
| 496 |
+
explicit CELUImpl(const CELUOptions& options_ = {});
|
| 497 |
+
|
| 498 |
+
Tensor forward(Tensor input);
|
| 499 |
+
|
| 500 |
+
void reset() override;
|
| 501 |
+
|
| 502 |
+
/// Pretty prints the `CELU` module into the given `stream`.
|
| 503 |
+
void pretty_print(std::ostream& stream) const override;
|
| 504 |
+
|
| 505 |
+
/// The options with which this `Module` was constructed.
|
| 506 |
+
CELUOptions options;
|
| 507 |
+
};
|
| 508 |
+
|
| 509 |
+
/// A `ModuleHolder` subclass for `CELUImpl`.
|
| 510 |
+
/// See the documentation for `CELUImpl` class to learn what methods it
|
| 511 |
+
/// provides, and examples of how to use `CELU` with `torch::nn::CELUOptions`.
|
| 512 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 513 |
+
/// module storage semantics.
|
| 514 |
+
TORCH_MODULE(CELU);
|
| 515 |
+
|
| 516 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GLU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 517 |
+
|
| 518 |
+
/// Applies glu over a given input.
|
| 519 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.GLU to learn
|
| 520 |
+
/// about the exact behavior of this module.
|
| 521 |
+
///
|
| 522 |
+
/// See the documentation for `torch::nn::GLUOptions` class to learn what
|
| 523 |
+
/// constructor arguments are supported for this module.
|
| 524 |
+
///
|
| 525 |
+
/// Example:
|
| 526 |
+
/// ```
|
| 527 |
+
/// GLU model(GLUOptions(1));
|
| 528 |
+
/// ```
|
| 529 |
+
class TORCH_API GLUImpl : public torch::nn::Cloneable<GLUImpl> {
|
| 530 |
+
public:
|
| 531 |
+
explicit GLUImpl(const GLUOptions& options_ = {});
|
| 532 |
+
|
| 533 |
+
Tensor forward(const Tensor& input);
|
| 534 |
+
|
| 535 |
+
void reset() override;
|
| 536 |
+
|
| 537 |
+
/// Pretty prints the `GLU` module into the given `stream`.
|
| 538 |
+
void pretty_print(std::ostream& stream) const override;
|
| 539 |
+
|
| 540 |
+
/// The options with which this `Module` was constructed.
|
| 541 |
+
GLUOptions options;
|
| 542 |
+
};
|
| 543 |
+
|
| 544 |
+
/// A `ModuleHolder` subclass for `GLUImpl`.
|
| 545 |
+
/// See the documentation for `GLUImpl` class to learn what methods it
|
| 546 |
+
/// provides, and examples of how to use `GLU` with `torch::nn::GLUOptions`.
|
| 547 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 548 |
+
/// module storage semantics.
|
| 549 |
+
TORCH_MODULE(GLU);
|
| 550 |
+
|
| 551 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GELU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 552 |
+
|
| 553 |
+
/// Applies gelu over a given input.
|
| 554 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.GELU to learn
|
| 555 |
+
/// about the exact behavior of this module.
|
| 556 |
+
class TORCH_API GELUImpl : public torch::nn::Cloneable<GELUImpl> {
|
| 557 |
+
public:
|
| 558 |
+
explicit GELUImpl(GELUOptions options_ = {});
|
| 559 |
+
|
| 560 |
+
Tensor forward(const Tensor& input);
|
| 561 |
+
|
| 562 |
+
void reset() override;
|
| 563 |
+
|
| 564 |
+
/// Pretty prints the `GELU` module into the given `stream`.
|
| 565 |
+
void pretty_print(std::ostream& stream) const override;
|
| 566 |
+
|
| 567 |
+
/// The options with which this `Module` was constructed.
|
| 568 |
+
GELUOptions options;
|
| 569 |
+
};
|
| 570 |
+
|
| 571 |
+
/// A `ModuleHolder` subclass for `GELUImpl`.
|
| 572 |
+
/// See the documentation for `GELUImpl` class to learn what methods it
|
| 573 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 574 |
+
/// module storage semantics.
|
| 575 |
+
TORCH_MODULE(GELU);
|
| 576 |
+
|
| 577 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SiLU ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 578 |
+
|
| 579 |
+
/// Applies silu over a given input.
|
| 580 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.SiLU to learn
|
| 581 |
+
/// about the exact behavior of this module.
|
| 582 |
+
class TORCH_API SiLUImpl : public torch::nn::Cloneable<SiLUImpl> {
|
| 583 |
+
public:
|
| 584 |
+
Tensor forward(const Tensor& input);
|
| 585 |
+
|
| 586 |
+
void reset() override;
|
| 587 |
+
|
| 588 |
+
/// Pretty prints the `SiLU` module into the given `stream`.
|
| 589 |
+
void pretty_print(std::ostream& stream) const override;
|
| 590 |
+
};
|
| 591 |
+
|
| 592 |
+
/// A `ModuleHolder` subclass for `SiLUImpl`.
|
| 593 |
+
/// See the documentation for `SiLUImpl` class to learn what methods it
|
| 594 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 595 |
+
/// module storage semantics.
|
| 596 |
+
TORCH_MODULE(SiLU);
|
| 597 |
+
|
| 598 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Mish ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 599 |
+
|
| 600 |
+
/// Applies mish over a given input.
|
| 601 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Mish to learn
|
| 602 |
+
/// about the exact behavior of this module.
|
| 603 |
+
class TORCH_API MishImpl : public torch::nn::Cloneable<MishImpl> {
|
| 604 |
+
public:
|
| 605 |
+
Tensor forward(const Tensor& input);
|
| 606 |
+
|
| 607 |
+
void reset() override;
|
| 608 |
+
|
| 609 |
+
/// Pretty prints the `Mish` module into the given `stream`.
|
| 610 |
+
void pretty_print(std::ostream& stream) const override;
|
| 611 |
+
};
|
| 612 |
+
|
| 613 |
+
/// A `ModuleHolder` subclass for `MishImpl`.
|
| 614 |
+
/// See the documentation for `MishImpl` class to learn what methods it
|
| 615 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 616 |
+
/// module storage semantics.
|
| 617 |
+
TORCH_MODULE(Mish);
|
| 618 |
+
|
| 619 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sigmoid ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 620 |
+
|
| 621 |
+
/// Applies sigmoid over a given input.
|
| 622 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Sigmoid to learn
|
| 623 |
+
/// about the exact behavior of this module.
|
| 624 |
+
class TORCH_API SigmoidImpl : public torch::nn::Cloneable<SigmoidImpl> {
|
| 625 |
+
public:
|
| 626 |
+
Tensor forward(const Tensor& input);
|
| 627 |
+
|
| 628 |
+
void reset() override;
|
| 629 |
+
|
| 630 |
+
/// Pretty prints the `Sigmoid` module into the given `stream`.
|
| 631 |
+
void pretty_print(std::ostream& stream) const override;
|
| 632 |
+
};
|
| 633 |
+
|
| 634 |
+
/// A `ModuleHolder` subclass for `SigmoidImpl`.
|
| 635 |
+
/// See the documentation for `SigmoidImpl` class to learn what methods it
|
| 636 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 637 |
+
/// module storage semantics.
|
| 638 |
+
TORCH_MODULE(Sigmoid);
|
| 639 |
+
|
| 640 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Softplus ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 641 |
+
|
| 642 |
+
/// Applies softplus over a given input.
|
| 643 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Softplus to learn
|
| 644 |
+
/// about the exact behavior of this module.
|
| 645 |
+
///
|
| 646 |
+
/// See the documentation for `torch::nn::SoftplusOptions` class to learn what
|
| 647 |
+
/// constructor arguments are supported for this module.
|
| 648 |
+
///
|
| 649 |
+
/// Example:
|
| 650 |
+
/// ```
|
| 651 |
+
/// Softplus model(SoftplusOptions().beta(0.24).threshold(42.42));
|
| 652 |
+
/// ```
|
| 653 |
+
class TORCH_API SoftplusImpl : public torch::nn::Cloneable<SoftplusImpl> {
|
| 654 |
+
public:
|
| 655 |
+
explicit SoftplusImpl(const SoftplusOptions& options_ = {});
|
| 656 |
+
|
| 657 |
+
Tensor forward(const Tensor& input);
|
| 658 |
+
|
| 659 |
+
void reset() override;
|
| 660 |
+
|
| 661 |
+
/// Pretty prints the `Softplus` module into the given `stream`.
|
| 662 |
+
void pretty_print(std::ostream& stream) const override;
|
| 663 |
+
|
| 664 |
+
/// The options with which this `Module` was constructed.
|
| 665 |
+
SoftplusOptions options;
|
| 666 |
+
};
|
| 667 |
+
|
| 668 |
+
/// A `ModuleHolder` subclass for `SoftplusImpl`.
|
| 669 |
+
/// See the documentation for `SoftplusImpl` class to learn what methods it
|
| 670 |
+
/// provides, and examples of how to use `Softplus` with
|
| 671 |
+
/// `torch::nn::SoftplusOptions`. See the documentation for `ModuleHolder` to
|
| 672 |
+
/// learn about PyTorch's module storage semantics.
|
| 673 |
+
TORCH_MODULE(Softplus);
|
| 674 |
+
|
| 675 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Softshrink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 676 |
+
|
| 677 |
+
/// Applies the soft shrinkage function element-wise.
|
| 678 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Softshrink to learn
|
| 679 |
+
/// about the exact behavior of this module.
|
| 680 |
+
///
|
| 681 |
+
/// See the documentation for `torch::nn::SoftshrinkOptions` class to learn what
|
| 682 |
+
/// constructor arguments are supported for this module.
|
| 683 |
+
///
|
| 684 |
+
/// Example:
|
| 685 |
+
/// ```
|
| 686 |
+
/// Softshrink model(SoftshrinkOptions(42.42));
|
| 687 |
+
/// ```
|
| 688 |
+
class TORCH_API SoftshrinkImpl : public torch::nn::Cloneable<SoftshrinkImpl> {
|
| 689 |
+
public:
|
| 690 |
+
explicit SoftshrinkImpl(const SoftshrinkOptions& options_ = {});
|
| 691 |
+
|
| 692 |
+
Tensor forward(const Tensor& input);
|
| 693 |
+
|
| 694 |
+
void reset() override;
|
| 695 |
+
|
| 696 |
+
/// Pretty prints the `Softshrink` module into the given `stream`.
|
| 697 |
+
void pretty_print(std::ostream& stream) const override;
|
| 698 |
+
|
| 699 |
+
/// The options with which this `Module` was constructed.
|
| 700 |
+
SoftshrinkOptions options;
|
| 701 |
+
};
|
| 702 |
+
|
| 703 |
+
/// A `ModuleHolder` subclass for `SoftshrinkImpl`.
|
| 704 |
+
/// See the documentation for `SoftshrinkImpl` class to learn what methods it
|
| 705 |
+
/// provides, and examples of how to use `Softshrink` with
|
| 706 |
+
/// `torch::nn::SoftshrinkOptions`. See the documentation for `ModuleHolder` to
|
| 707 |
+
/// learn about PyTorch's module storage semantics.
|
| 708 |
+
TORCH_MODULE(Softshrink);
|
| 709 |
+
|
| 710 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Softsign ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 711 |
+
|
| 712 |
+
/// Applies Softsign over a given input.
|
| 713 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Softsign to learn
|
| 714 |
+
/// about the exact behavior of this module.
|
| 715 |
+
class TORCH_API SoftsignImpl : public torch::nn::Cloneable<SoftsignImpl> {
|
| 716 |
+
public:
|
| 717 |
+
Tensor forward(const Tensor& input);
|
| 718 |
+
|
| 719 |
+
void reset() override;
|
| 720 |
+
|
| 721 |
+
/// Pretty prints the `Softsign` module into the given `stream`.
|
| 722 |
+
void pretty_print(std::ostream& stream) const override;
|
| 723 |
+
};
|
| 724 |
+
|
| 725 |
+
/// A `ModuleHolder` subclass for `SoftsignImpl`.
|
| 726 |
+
/// See the documentation for `SoftsignImpl` class to learn what methods it
|
| 727 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 728 |
+
/// module storage semantics.
|
| 729 |
+
TORCH_MODULE(Softsign);
|
| 730 |
+
|
| 731 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tanh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 732 |
+
|
| 733 |
+
/// Applies Tanh over a given input.
|
| 734 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Tanh to learn
|
| 735 |
+
/// about the exact behavior of this module.
|
| 736 |
+
class TORCH_API TanhImpl : public torch::nn::Cloneable<TanhImpl> {
|
| 737 |
+
public:
|
| 738 |
+
Tensor forward(const Tensor& input);
|
| 739 |
+
|
| 740 |
+
void reset() override;
|
| 741 |
+
|
| 742 |
+
/// Pretty prints the `Tanh` module into the given `stream`.
|
| 743 |
+
void pretty_print(std::ostream& stream) const override;
|
| 744 |
+
};
|
| 745 |
+
|
| 746 |
+
/// A `ModuleHolder` subclass for `TanhImpl`.
|
| 747 |
+
/// See the documentation for `TanhImpl` class to learn what methods it
|
| 748 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 749 |
+
/// module storage semantics.
|
| 750 |
+
TORCH_MODULE(Tanh);
|
| 751 |
+
|
| 752 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tanhshrink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 753 |
+
|
| 754 |
+
/// Applies Tanhshrink over a given input.
|
| 755 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Tanhshrink to learn
|
| 756 |
+
/// about the exact behavior of this module.
|
| 757 |
+
class TORCH_API TanhshrinkImpl : public torch::nn::Cloneable<TanhshrinkImpl> {
|
| 758 |
+
public:
|
| 759 |
+
Tensor forward(const Tensor& input);
|
| 760 |
+
|
| 761 |
+
void reset() override;
|
| 762 |
+
|
| 763 |
+
/// Pretty prints the `Tanhshrink` module into the given `stream`.
|
| 764 |
+
void pretty_print(std::ostream& stream) const override;
|
| 765 |
+
};
|
| 766 |
+
|
| 767 |
+
/// A `ModuleHolder` subclass for `TanhshrinkImpl`.
|
| 768 |
+
/// See the documentation for `TanhshrinkImpl` class to learn what methods it
|
| 769 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 770 |
+
/// module storage semantics.
|
| 771 |
+
TORCH_MODULE(Tanhshrink);
|
| 772 |
+
|
| 773 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Threshold ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 774 |
+
|
| 775 |
+
/// Applies the Threshold function element-wise.
|
| 776 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Threshold to learn
|
| 777 |
+
/// about the exact behavior of this module.
|
| 778 |
+
///
|
| 779 |
+
/// See the documentation for `torch::nn::ThresholdOptions` class to learn what
|
| 780 |
+
/// constructor arguments are supported for this module.
|
| 781 |
+
///
|
| 782 |
+
/// Example:
|
| 783 |
+
/// ```
|
| 784 |
+
/// Threshold model(ThresholdOptions(42.42, 24.24).inplace(true));
|
| 785 |
+
/// ```
|
| 786 |
+
class TORCH_API ThresholdImpl : public torch::nn::Cloneable<ThresholdImpl> {
|
| 787 |
+
public:
|
| 788 |
+
ThresholdImpl(double threshold, double value)
|
| 789 |
+
: ThresholdImpl(ThresholdOptions(threshold, value)) {}
|
| 790 |
+
explicit ThresholdImpl(const ThresholdOptions& options_);
|
| 791 |
+
|
| 792 |
+
Tensor forward(Tensor input);
|
| 793 |
+
|
| 794 |
+
void reset() override;
|
| 795 |
+
|
| 796 |
+
/// Pretty prints the `Threshold` module into the given `stream`.
|
| 797 |
+
void pretty_print(std::ostream& stream) const override;
|
| 798 |
+
|
| 799 |
+
/// The options with which this `Module` was constructed.
|
| 800 |
+
ThresholdOptions options;
|
| 801 |
+
};
|
| 802 |
+
|
| 803 |
+
/// A `ModuleHolder` subclass for `ThresholdImpl`.
|
| 804 |
+
/// See the documentation for `ThresholdImpl` class to learn what methods it
|
| 805 |
+
/// provides, and examples of how to use `Threshold` with
|
| 806 |
+
/// `torch::nn::ThresholdOptions`. See the documentation for `ModuleHolder` to
|
| 807 |
+
/// learn about PyTorch's module storage semantics.
|
| 808 |
+
TORCH_MODULE(Threshold);
|
| 809 |
+
|
| 810 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MultiheadAttention ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 811 |
+
|
| 812 |
+
/// Applies the MultiheadAttention function element-wise.
|
| 813 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.MultiheadAttention
|
| 814 |
+
/// to learn about the exact behavior of this module.
|
| 815 |
+
///
|
| 816 |
+
/// See the documentation for `torch::nn::MultiheadAttentionOptions` class to
|
| 817 |
+
/// learn what constructor arguments are supported for this module.
|
| 818 |
+
///
|
| 819 |
+
/// Example:
|
| 820 |
+
/// ```
|
| 821 |
+
/// MultiheadAttention model(MultiheadAttentionOptions(20, 10).bias(false));
|
| 822 |
+
/// ```
|
| 823 |
+
class TORCH_API MultiheadAttentionImpl
|
| 824 |
+
: public torch::nn::Cloneable<MultiheadAttentionImpl> {
|
| 825 |
+
public:
|
| 826 |
+
MultiheadAttentionImpl(int64_t embed_dim, int64_t num_heads)
|
| 827 |
+
: MultiheadAttentionImpl(
|
| 828 |
+
MultiheadAttentionOptions(embed_dim, num_heads)) {}
|
| 829 |
+
explicit MultiheadAttentionImpl(const MultiheadAttentionOptions& options_);
|
| 830 |
+
|
| 831 |
+
std::tuple<Tensor, Tensor> forward(
|
| 832 |
+
const Tensor& query,
|
| 833 |
+
const Tensor& key,
|
| 834 |
+
const Tensor& value,
|
| 835 |
+
const Tensor& key_padding_mask = {},
|
| 836 |
+
bool need_weights = true,
|
| 837 |
+
const Tensor& attn_mask = {},
|
| 838 |
+
bool average_attn_weights = true);
|
| 839 |
+
|
| 840 |
+
protected:
|
| 841 |
+
FORWARD_HAS_DEFAULT_ARGS(
|
| 842 |
+
{3, AnyValue(Tensor())},
|
| 843 |
+
{4, AnyValue(true)},
|
| 844 |
+
{5, AnyValue(Tensor())},
|
| 845 |
+
{6, AnyValue(true)})
|
| 846 |
+
|
| 847 |
+
public:
|
| 848 |
+
void reset() override;
|
| 849 |
+
|
| 850 |
+
void _reset_parameters();
|
| 851 |
+
|
| 852 |
+
/// The options with which this `Module` was constructed.
|
| 853 |
+
MultiheadAttentionOptions options;
|
| 854 |
+
|
| 855 |
+
bool _qkv_same_embed_dim;
|
| 856 |
+
Tensor in_proj_weight;
|
| 857 |
+
Tensor in_proj_bias;
|
| 858 |
+
Tensor bias_k;
|
| 859 |
+
Tensor bias_v;
|
| 860 |
+
Linear out_proj = nullptr;
|
| 861 |
+
Tensor q_proj_weight;
|
| 862 |
+
Tensor k_proj_weight;
|
| 863 |
+
Tensor v_proj_weight;
|
| 864 |
+
int64_t head_dim;
|
| 865 |
+
};
|
| 866 |
+
|
| 867 |
+
/// A `ModuleHolder` subclass for `MultiheadAttentionImpl`.
|
| 868 |
+
/// See the documentation for `MultiheadAttentionImpl` class to learn what
|
| 869 |
+
/// methods it provides, and examples of how to use `MultiheadAttention` with
|
| 870 |
+
/// `torch::nn::MultiheadAttentionOptions`. See the documentation for
|
| 871 |
+
/// `ModuleHolder` to learn about PyTorch's module storage semantics.
|
| 872 |
+
TORCH_MODULE(MultiheadAttention);
|
| 873 |
+
|
| 874 |
+
} // namespace nn
|
| 875 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/adaptive.h
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/cloneable.h>
|
| 4 |
+
#include <torch/nn/functional/activation.h>
|
| 5 |
+
#include <torch/nn/module.h>
|
| 6 |
+
#include <torch/nn/modules/container/modulelist.h>
|
| 7 |
+
#include <torch/nn/modules/container/sequential.h>
|
| 8 |
+
#include <torch/nn/modules/linear.h>
|
| 9 |
+
#include <torch/nn/options/adaptive.h>
|
| 10 |
+
|
| 11 |
+
namespace torch {
|
| 12 |
+
namespace nn {
|
| 13 |
+
|
| 14 |
+
/// The output of a single invocation of an AdaptiveLogSoftmaxWithLoss
|
| 15 |
+
/// module's `forward()` method.
|
| 16 |
+
struct TORCH_API ASMoutput {
|
| 17 |
+
ASMoutput(Tensor output_, double loss_);
|
| 18 |
+
|
| 19 |
+
/// Tensor containing computed target log probabilities for each example
|
| 20 |
+
Tensor output;
|
| 21 |
+
|
| 22 |
+
/// Scalar representing the computed negative log likelihood loss
|
| 23 |
+
double loss;
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AdaptiveLogSoftmaxWithLoss
|
| 27 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 28 |
+
|
| 29 |
+
/// Efficient softmax approximation as described in
|
| 30 |
+
/// `Efficient softmax approximation for GPUs`_ by Edouard Grave, Armand Joulin,
|
| 31 |
+
/// Moustapha Cissé, David Grangier, and Hervé Jégou.
|
| 32 |
+
/// See
|
| 33 |
+
/// https://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveLogSoftmaxWithLoss
|
| 34 |
+
/// to learn about the exact behavior of this module.
|
| 35 |
+
///
|
| 36 |
+
/// See the documentation for `torch::nn::AdaptiveLogSoftmaxWithLossOptions`
|
| 37 |
+
/// class to learn what constructor arguments are supported for this module.
|
| 38 |
+
///
|
| 39 |
+
/// Example:
|
| 40 |
+
/// ```
|
| 41 |
+
/// AdaptiveLogSoftmaxWithLoss model(AdaptiveLogSoftmaxWithLossOptions(8, 10,
|
| 42 |
+
/// {4, 8}).div_value(2.).head_bias(true));
|
| 43 |
+
/// ```
|
| 44 |
+
class TORCH_API AdaptiveLogSoftmaxWithLossImpl
|
| 45 |
+
: public Cloneable<AdaptiveLogSoftmaxWithLossImpl> {
|
| 46 |
+
public:
|
| 47 |
+
AdaptiveLogSoftmaxWithLossImpl(
|
| 48 |
+
int64_t in_features,
|
| 49 |
+
int64_t n_classes,
|
| 50 |
+
std::vector<int64_t> cutoffs)
|
| 51 |
+
: AdaptiveLogSoftmaxWithLossImpl(AdaptiveLogSoftmaxWithLossOptions(
|
| 52 |
+
in_features,
|
| 53 |
+
n_classes,
|
| 54 |
+
cutoffs)) {}
|
| 55 |
+
|
| 56 |
+
explicit AdaptiveLogSoftmaxWithLossImpl(
|
| 57 |
+
AdaptiveLogSoftmaxWithLossOptions options_);
|
| 58 |
+
|
| 59 |
+
ASMoutput forward(const Tensor& input, const Tensor& target);
|
| 60 |
+
|
| 61 |
+
void reset() override;
|
| 62 |
+
|
| 63 |
+
void reset_parameters();
|
| 64 |
+
|
| 65 |
+
/// Pretty prints the `AdaptiveLogSoftmaxWithLoss` module into the given
|
| 66 |
+
/// `stream`.
|
| 67 |
+
void pretty_print(std::ostream& stream) const override;
|
| 68 |
+
|
| 69 |
+
/// Given input tensor, and output of `head`, computes the log of the full
|
| 70 |
+
/// distribution
|
| 71 |
+
Tensor _get_full_log_prob(const Tensor& input, const Tensor& head_output);
|
| 72 |
+
|
| 73 |
+
/// Computes log probabilities for all n_classes
|
| 74 |
+
Tensor log_prob(const Tensor& input);
|
| 75 |
+
|
| 76 |
+
/// This is equivalent to `log_pob(input).argmax(1)` but is more efficient in
|
| 77 |
+
/// some cases
|
| 78 |
+
Tensor predict(const Tensor& input);
|
| 79 |
+
|
| 80 |
+
/// The options with which this `Module` was constructed
|
| 81 |
+
AdaptiveLogSoftmaxWithLossOptions options;
|
| 82 |
+
|
| 83 |
+
/// Cutoffs used to assign targets to their buckets. It should be an ordered
|
| 84 |
+
/// Sequence of integers sorted in the increasing order
|
| 85 |
+
std::vector<int64_t> cutoffs;
|
| 86 |
+
|
| 87 |
+
int64_t shortlist_size;
|
| 88 |
+
|
| 89 |
+
/// Number of clusters
|
| 90 |
+
int64_t n_clusters;
|
| 91 |
+
|
| 92 |
+
/// Output size of head classifier
|
| 93 |
+
int64_t head_size;
|
| 94 |
+
|
| 95 |
+
Linear head = nullptr;
|
| 96 |
+
|
| 97 |
+
ModuleList tail;
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
/// A `ModuleHolder` subclass for `AdaptiveLogSoftmaxWithLossImpl`.
|
| 101 |
+
/// See the documentation for `AdaptiveLogSoftmaxWithLossImpl` class to learn
|
| 102 |
+
/// what methods it provides, and examples of how to use
|
| 103 |
+
/// `AdaptiveLogSoftmaxWithLoss` with
|
| 104 |
+
/// `torch::nn::AdaptiveLogSoftmaxWithLossOptions`. See the documentation for
|
| 105 |
+
/// `ModuleHolder` to learn about PyTorch's module storage semantics.
|
| 106 |
+
TORCH_MODULE(AdaptiveLogSoftmaxWithLoss);
|
| 107 |
+
|
| 108 |
+
} // namespace nn
|
| 109 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/batchnorm.h
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/cloneable.h>
|
| 4 |
+
#include <torch/nn/functional/batchnorm.h>
|
| 5 |
+
#include <torch/nn/init.h>
|
| 6 |
+
#include <torch/nn/options/batchnorm.h>
|
| 7 |
+
#include <torch/nn/pimpl.h>
|
| 8 |
+
#include <torch/types.h>
|
| 9 |
+
|
| 10 |
+
#include <cstdint>
|
| 11 |
+
|
| 12 |
+
namespace torch {
|
| 13 |
+
namespace nn {
|
| 14 |
+
|
| 15 |
+
/// Base class for all (dimension-specialized) batchnorm and instancenorm
|
| 16 |
+
/// modules.
|
| 17 |
+
template <size_t D, typename Derived, typename DerivedOptions>
|
| 18 |
+
class NormImplBase : public torch::nn::Cloneable<Derived> {
|
| 19 |
+
protected:
|
| 20 |
+
virtual void _check_input_dim(const Tensor& input) = 0;
|
| 21 |
+
|
| 22 |
+
public:
|
| 23 |
+
NormImplBase(const DerivedOptions& options_) : options(options_) {
|
| 24 |
+
// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
|
| 25 |
+
reset();
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
void reset() override {
|
| 29 |
+
if (options.affine()) {
|
| 30 |
+
weight = this->register_parameter(
|
| 31 |
+
"weight", torch::empty({options.num_features()}));
|
| 32 |
+
bias = this->register_parameter(
|
| 33 |
+
"bias", torch::empty({options.num_features()}));
|
| 34 |
+
} else {
|
| 35 |
+
weight =
|
| 36 |
+
this->register_parameter("weight", Tensor(), /*requires_grad=*/false);
|
| 37 |
+
bias =
|
| 38 |
+
this->register_parameter("bias", Tensor(), /*requires_grad=*/false);
|
| 39 |
+
}
|
| 40 |
+
if (options.track_running_stats()) {
|
| 41 |
+
running_mean = this->register_buffer(
|
| 42 |
+
"running_mean", torch::zeros({options.num_features()}));
|
| 43 |
+
running_var = this->register_buffer(
|
| 44 |
+
"running_var", torch::ones({options.num_features()}));
|
| 45 |
+
num_batches_tracked = this->register_buffer(
|
| 46 |
+
"num_batches_tracked", torch::tensor(0, torch::dtype(torch::kLong)));
|
| 47 |
+
} else {
|
| 48 |
+
running_mean = this->register_buffer("running_mean", Tensor());
|
| 49 |
+
running_var = this->register_buffer("running_var", Tensor());
|
| 50 |
+
num_batches_tracked =
|
| 51 |
+
this->register_buffer("num_batches_tracked", Tensor());
|
| 52 |
+
}
|
| 53 |
+
reset_parameters();
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
void reset_running_stats() {
|
| 57 |
+
if (options.track_running_stats()) {
|
| 58 |
+
running_mean.zero_();
|
| 59 |
+
running_var.fill_(1);
|
| 60 |
+
num_batches_tracked.zero_();
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
void reset_parameters() {
|
| 65 |
+
reset_running_stats();
|
| 66 |
+
if (options.affine()) {
|
| 67 |
+
torch::nn::init::ones_(weight);
|
| 68 |
+
torch::nn::init::zeros_(bias);
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/// The options with which this module was constructed.
|
| 73 |
+
DerivedOptions options;
|
| 74 |
+
|
| 75 |
+
/// The learned weight.
|
| 76 |
+
/// Only defined if the `affine` option was `true` upon construction.
|
| 77 |
+
Tensor weight;
|
| 78 |
+
|
| 79 |
+
/// The learned bias.
|
| 80 |
+
/// Only defined if the `affine` option was `true` upon construction.
|
| 81 |
+
Tensor bias;
|
| 82 |
+
|
| 83 |
+
/// The running mean.
|
| 84 |
+
/// Only defined if the `track_running_stats` option was `true` upon
|
| 85 |
+
/// construction.
|
| 86 |
+
Tensor running_mean;
|
| 87 |
+
|
| 88 |
+
/// The running variance.
|
| 89 |
+
/// Only defined if the `track_running_stats` option was `true` upon
|
| 90 |
+
/// construction.
|
| 91 |
+
Tensor running_var;
|
| 92 |
+
|
| 93 |
+
/// The number of the forward call.
|
| 94 |
+
/// Only defined if the `track_running_stats` option was `true` upon
|
| 95 |
+
/// construction.
|
| 96 |
+
Tensor num_batches_tracked;
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
+
/// Base class for all (dimension-specialized) batchnorm modules.
|
| 100 |
+
template <size_t D, typename Derived>
|
| 101 |
+
class BatchNormImplBase : public NormImplBase<D, Derived, BatchNormOptions> {
|
| 102 |
+
public:
|
| 103 |
+
using NormImplBase<D, Derived, BatchNormOptions>::NormImplBase;
|
| 104 |
+
|
| 105 |
+
Tensor forward(const Tensor& input) {
|
| 106 |
+
this->_check_input_dim(input);
|
| 107 |
+
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
| 108 |
+
double exponential_average_factor;
|
| 109 |
+
if (this->options.momentum() == c10::nullopt) {
|
| 110 |
+
exponential_average_factor = 0.0;
|
| 111 |
+
} else {
|
| 112 |
+
exponential_average_factor = this->options.momentum().value();
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
if (this->is_training() && this->options.track_running_stats()) {
|
| 116 |
+
if (this->num_batches_tracked.defined()) {
|
| 117 |
+
this->num_batches_tracked += 1;
|
| 118 |
+
if (this->options.momentum() ==
|
| 119 |
+
c10::nullopt) { // use cumulative moving average
|
| 120 |
+
exponential_average_factor =
|
| 121 |
+
1.0 / this->num_batches_tracked.template item<double>();
|
| 122 |
+
} else { // use exponential moving average
|
| 123 |
+
exponential_average_factor = this->options.momentum().value();
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
return torch::nn::functional::detail::batch_norm(
|
| 129 |
+
input,
|
| 130 |
+
this->running_mean,
|
| 131 |
+
this->running_var,
|
| 132 |
+
this->weight,
|
| 133 |
+
this->bias,
|
| 134 |
+
this->is_training() || !this->options.track_running_stats(),
|
| 135 |
+
/*momentum=*/exponential_average_factor,
|
| 136 |
+
this->options.eps());
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
/// Pretty prints the `BatchNorm{1,2,3}d` module into the given `stream`.
|
| 140 |
+
void pretty_print(std::ostream& stream) const override {
|
| 141 |
+
stream << std::boolalpha << "torch::nn::BatchNorm" << D << "d("
|
| 142 |
+
<< this->options.num_features() << ", "
|
| 143 |
+
<< "eps=" << this->options.eps() << ", "
|
| 144 |
+
<< "momentum=";
|
| 145 |
+
|
| 146 |
+
if (this->options.momentum().has_value()) {
|
| 147 |
+
stream << this->options.momentum().value();
|
| 148 |
+
} else {
|
| 149 |
+
stream << "None";
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
stream << ", "
|
| 153 |
+
<< "affine=" << this->options.affine() << ", "
|
| 154 |
+
<< "track_running_stats=" << this->options.track_running_stats()
|
| 155 |
+
<< ")";
|
| 156 |
+
}
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BatchNorm1d
|
| 160 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 161 |
+
|
| 162 |
+
/// Applies the BatchNorm1d function.
|
| 163 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.BatchNorm1d to learn
|
| 164 |
+
/// about the exact behavior of this module.
|
| 165 |
+
///
|
| 166 |
+
/// See the documentation for `torch::nn::BatchNorm1dOptions` class to learn
|
| 167 |
+
/// what constructor arguments are supported for this module.
|
| 168 |
+
///
|
| 169 |
+
/// Example:
|
| 170 |
+
/// ```
|
| 171 |
+
/// BatchNorm1d
|
| 172 |
+
/// model(BatchNorm1dOptions(4).eps(0.5).momentum(0.1).affine(false).track_running_stats(true));
|
| 173 |
+
/// ```
|
| 174 |
+
class TORCH_API BatchNorm1dImpl : public BatchNormImplBase<1, BatchNorm1dImpl> {
|
| 175 |
+
protected:
|
| 176 |
+
void _check_input_dim(const Tensor& input) override;
|
| 177 |
+
|
| 178 |
+
public:
|
| 179 |
+
using BatchNormImplBase<1, BatchNorm1dImpl>::BatchNormImplBase;
|
| 180 |
+
};
|
| 181 |
+
|
| 182 |
+
/// A `ModuleHolder` subclass for `BatchNorm1dImpl`.
|
| 183 |
+
/// See the documentation for `BatchNorm1dImpl` class to learn what methods it
|
| 184 |
+
/// provides, and examples of how to use `BatchNorm1d` with
|
| 185 |
+
/// `torch::nn::BatchNorm1dOptions`. See the documentation for `ModuleHolder` to
|
| 186 |
+
/// learn about PyTorch's module storage semantics.
|
| 187 |
+
TORCH_MODULE(BatchNorm1d);
|
| 188 |
+
|
| 189 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BatchNorm2d
|
| 190 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 191 |
+
|
| 192 |
+
/// Applies the BatchNorm2d function.
|
| 193 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.BatchNorm2d to learn
|
| 194 |
+
/// about the exact behavior of this module.
|
| 195 |
+
///
|
| 196 |
+
/// See the documentation for `torch::nn::BatchNorm2dOptions` class to learn
|
| 197 |
+
/// what constructor arguments are supported for this module.
|
| 198 |
+
///
|
| 199 |
+
/// Example:
|
| 200 |
+
/// ```
|
| 201 |
+
/// BatchNorm2d
|
| 202 |
+
/// model(BatchNorm2dOptions(4).eps(0.5).momentum(0.1).affine(false).track_running_stats(true));
|
| 203 |
+
/// ```
|
| 204 |
+
class TORCH_API BatchNorm2dImpl : public BatchNormImplBase<2, BatchNorm2dImpl> {
|
| 205 |
+
protected:
|
| 206 |
+
void _check_input_dim(const Tensor& input) override;
|
| 207 |
+
|
| 208 |
+
public:
|
| 209 |
+
using BatchNormImplBase<2, BatchNorm2dImpl>::BatchNormImplBase;
|
| 210 |
+
};
|
| 211 |
+
|
| 212 |
+
/// A `ModuleHolder` subclass for `BatchNorm2dImpl`.
|
| 213 |
+
/// See the documentation for `BatchNorm2dImpl` class to learn what methods it
|
| 214 |
+
/// provides, and examples of how to use `BatchNorm2d` with
|
| 215 |
+
/// `torch::nn::BatchNorm2dOptions`. See the documentation for `ModuleHolder` to
|
| 216 |
+
/// learn about PyTorch's module storage semantics.
|
| 217 |
+
TORCH_MODULE(BatchNorm2d);
|
| 218 |
+
|
| 219 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BatchNorm3d
|
| 220 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 221 |
+
|
| 222 |
+
/// Applies the BatchNorm3d function.
|
| 223 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.BatchNorm3d to learn
|
| 224 |
+
/// about the exact behavior of this module.
|
| 225 |
+
///
|
| 226 |
+
/// See the documentation for `torch::nn::BatchNorm3dOptions` class to learn
|
| 227 |
+
/// what constructor arguments are supported for this module.
|
| 228 |
+
///
|
| 229 |
+
/// Example:
|
| 230 |
+
/// ```
|
| 231 |
+
/// BatchNorm3d
|
| 232 |
+
/// model(BatchNorm3dOptions(4).eps(0.5).momentum(0.1).affine(false).track_running_stats(true));
|
| 233 |
+
/// ```
|
| 234 |
+
class TORCH_API BatchNorm3dImpl : public BatchNormImplBase<3, BatchNorm3dImpl> {
|
| 235 |
+
protected:
|
| 236 |
+
void _check_input_dim(const Tensor& input) override;
|
| 237 |
+
|
| 238 |
+
public:
|
| 239 |
+
using BatchNormImplBase<3, BatchNorm3dImpl>::BatchNormImplBase;
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
/// A `ModuleHolder` subclass for `BatchNorm3dImpl`.
|
| 243 |
+
/// See the documentation for `BatchNorm3dImpl` class to learn what methods it
|
| 244 |
+
/// provides, and examples of how to use `BatchNorm3d` with
|
| 245 |
+
/// `torch::nn::BatchNorm3dOptions`. See the documentation for `ModuleHolder` to
|
| 246 |
+
/// learn about PyTorch's module storage semantics.
|
| 247 |
+
TORCH_MODULE(BatchNorm3d);
|
| 248 |
+
|
| 249 |
+
} // namespace nn
|
| 250 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/any.h
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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/any_value.h>
|
| 7 |
+
#include <torch/nn/pimpl.h>
|
| 8 |
+
#include <torch/types.h>
|
| 9 |
+
|
| 10 |
+
#include <torch/csrc/autograd/variable.h>
|
| 11 |
+
#include <torch/csrc/utils/variadic.h>
|
| 12 |
+
|
| 13 |
+
#include <ATen/Device.h>
|
| 14 |
+
|
| 15 |
+
#include <memory>
|
| 16 |
+
#include <type_traits>
|
| 17 |
+
#include <typeinfo>
|
| 18 |
+
#include <utility>
|
| 19 |
+
#include <vector>
|
| 20 |
+
|
| 21 |
+
namespace torch {
|
| 22 |
+
namespace nn {
|
| 23 |
+
|
| 24 |
+
/// Stores a type erased `Module`.
|
| 25 |
+
///
|
| 26 |
+
/// The PyTorch C++ API does not impose an interface on the signature of
|
| 27 |
+
/// `forward()` in `Module` subclasses. This gives you complete freedom to
|
| 28 |
+
/// design your `forward()` methods to your liking. However, this also means
|
| 29 |
+
/// there is no unified base type you could store in order to call `forward()`
|
| 30 |
+
/// polymorphically for any module. This is where the `AnyModule` comes in.
|
| 31 |
+
/// Instead of inheritance, it relies on type erasure for polymorphism.
|
| 32 |
+
///
|
| 33 |
+
/// An `AnyModule` can store any `nn::Module` subclass that provides a
|
| 34 |
+
/// `forward()` method. This `forward()` may accept any types and return any
|
| 35 |
+
/// type. Once stored in an `AnyModule`, you can invoke the underlying module's
|
| 36 |
+
/// `forward()` by calling `AnyModule::forward()` with the arguments you would
|
| 37 |
+
/// supply to the stored module (though see one important limitation below).
|
| 38 |
+
/// Example:
|
| 39 |
+
///
|
| 40 |
+
/// \rst
|
| 41 |
+
/// .. code-block:: cpp
|
| 42 |
+
///
|
| 43 |
+
/// struct GenericTrainer {
|
| 44 |
+
/// torch::nn::AnyModule module;
|
| 45 |
+
///
|
| 46 |
+
/// void train(torch::Tensor input) {
|
| 47 |
+
/// module.forward(input);
|
| 48 |
+
/// }
|
| 49 |
+
/// };
|
| 50 |
+
///
|
| 51 |
+
/// GenericTrainer trainer1{torch::nn::Linear(3, 4)};
|
| 52 |
+
/// GenericTrainer trainer2{torch::nn::Conv2d(3, 4, 2)};
|
| 53 |
+
/// \endrst
|
| 54 |
+
///
|
| 55 |
+
/// As `AnyModule` erases the static type of the stored module (and its
|
| 56 |
+
/// `forward()` method) to achieve polymorphism, type checking of arguments is
|
| 57 |
+
/// moved to runtime. That is, passing an argument with an incorrect type to an
|
| 58 |
+
/// `AnyModule` will compile, but throw an exception at runtime:
|
| 59 |
+
///
|
| 60 |
+
/// \rst
|
| 61 |
+
/// .. code-block:: cpp
|
| 62 |
+
///
|
| 63 |
+
/// torch::nn::AnyModule module(torch::nn::Linear(3, 4));
|
| 64 |
+
/// // Linear takes a tensor as input, but we are passing an integer.
|
| 65 |
+
/// // This will compile, but throw a `torch::Error` exception at runtime.
|
| 66 |
+
/// module.forward(123);
|
| 67 |
+
/// \endrst
|
| 68 |
+
///
|
| 69 |
+
/// \rst
|
| 70 |
+
/// .. attention::
|
| 71 |
+
/// One noteworthy limitation of `AnyModule` is that its `forward()` method
|
| 72 |
+
/// does not support implicit conversion of argument types. For example, if
|
| 73 |
+
/// the stored module's `forward()` method accepts a `float` and you call
|
| 74 |
+
/// `any_module.forward(3.4)` (where `3.4` is a `double`), this will throw
|
| 75 |
+
/// an exception.
|
| 76 |
+
/// \endrst
|
| 77 |
+
///
|
| 78 |
+
/// The return type of the `AnyModule`'s `forward()` method is controlled via
|
| 79 |
+
/// the first template argument to `AnyModule::forward()`. It defaults to
|
| 80 |
+
/// `torch::Tensor`. To change it, you can write `any_module.forward<int>()`,
|
| 81 |
+
/// for example.
|
| 82 |
+
///
|
| 83 |
+
/// \rst
|
| 84 |
+
/// .. code-block:: cpp
|
| 85 |
+
///
|
| 86 |
+
/// torch::nn::AnyModule module(torch::nn::Linear(3, 4));
|
| 87 |
+
/// auto output = module.forward(torch::ones({2, 3}));
|
| 88 |
+
///
|
| 89 |
+
/// struct IntModule {
|
| 90 |
+
/// int forward(int x) { return x; }
|
| 91 |
+
/// };
|
| 92 |
+
/// torch::nn::AnyModule module(IntModule{});
|
| 93 |
+
/// int output = module.forward<int>(5);
|
| 94 |
+
/// \endrst
|
| 95 |
+
///
|
| 96 |
+
/// The only other method an `AnyModule` provides access to on the stored
|
| 97 |
+
/// module is `clone()`. However, you may acquire a handle on the module via
|
| 98 |
+
/// `.ptr()`, which returns a `shared_ptr<nn::Module>`. Further, if you know
|
| 99 |
+
/// the concrete type of the stored module, you can get a concrete handle to it
|
| 100 |
+
/// using `.get<T>()` where `T` is the concrete module type.
|
| 101 |
+
///
|
| 102 |
+
/// \rst
|
| 103 |
+
/// .. code-block:: cpp
|
| 104 |
+
///
|
| 105 |
+
/// torch::nn::AnyModule module(torch::nn::Linear(3, 4));
|
| 106 |
+
/// std::shared_ptr<nn::Module> ptr = module.ptr();
|
| 107 |
+
/// torch::nn::Linear linear(module.get<torch::nn::Linear>());
|
| 108 |
+
/// \endrst
|
| 109 |
+
class AnyModule {
|
| 110 |
+
public:
|
| 111 |
+
/// A default-constructed `AnyModule` is in an empty state.
|
| 112 |
+
AnyModule() = default;
|
| 113 |
+
|
| 114 |
+
/// Constructs an `AnyModule` from a `shared_ptr` to concrete module object.
|
| 115 |
+
template <typename ModuleType>
|
| 116 |
+
explicit AnyModule(std::shared_ptr<ModuleType> module);
|
| 117 |
+
|
| 118 |
+
/// Constructs an `AnyModule` from a concrete module object.
|
| 119 |
+
template <
|
| 120 |
+
typename ModuleType,
|
| 121 |
+
typename = torch::detail::enable_if_module_t<ModuleType>>
|
| 122 |
+
explicit AnyModule(ModuleType&& module);
|
| 123 |
+
|
| 124 |
+
/// Constructs an `AnyModule` from a module holder.
|
| 125 |
+
template <typename ModuleType>
|
| 126 |
+
explicit AnyModule(const ModuleHolder<ModuleType>& module_holder);
|
| 127 |
+
|
| 128 |
+
/// Move construction and assignment is allowed, and follows the default
|
| 129 |
+
/// behavior of move for `std::unique_ptr`.
|
| 130 |
+
AnyModule(AnyModule&&) = default;
|
| 131 |
+
AnyModule& operator=(AnyModule&&) = default;
|
| 132 |
+
|
| 133 |
+
/// Creates a shallow copy of an `AnyModule`.
|
| 134 |
+
AnyModule(const AnyModule& other);
|
| 135 |
+
AnyModule& operator=(const AnyModule& other);
|
| 136 |
+
|
| 137 |
+
/// Creates a deep copy of an `AnyModule` if it contains a module, else an
|
| 138 |
+
/// empty `AnyModule` if it is empty.
|
| 139 |
+
AnyModule clone(optional<Device> device = nullopt) const;
|
| 140 |
+
|
| 141 |
+
/// Assigns a module to the `AnyModule` (to circumvent the explicit
|
| 142 |
+
/// constructor).
|
| 143 |
+
template <typename ModuleType>
|
| 144 |
+
AnyModule& operator=(std::shared_ptr<ModuleType> module);
|
| 145 |
+
|
| 146 |
+
/// Invokes `forward()` on the contained module with the given arguments, and
|
| 147 |
+
/// returns the return value as an `AnyValue`. Use this method when chaining
|
| 148 |
+
/// `AnyModule`s in a loop.
|
| 149 |
+
template <typename... ArgumentTypes>
|
| 150 |
+
AnyValue any_forward(ArgumentTypes&&... arguments);
|
| 151 |
+
|
| 152 |
+
/// Invokes `forward()` on the contained module with the given arguments, and
|
| 153 |
+
/// casts the returned `AnyValue` to the supplied `ReturnType` (which defaults
|
| 154 |
+
/// to `torch::Tensor`).
|
| 155 |
+
template <typename ReturnType = torch::Tensor, typename... ArgumentTypes>
|
| 156 |
+
ReturnType forward(ArgumentTypes&&... arguments);
|
| 157 |
+
|
| 158 |
+
/// Attempts to cast the underlying module to the given module type. Throws an
|
| 159 |
+
/// exception if the types do not match.
|
| 160 |
+
template <typename T, typename = torch::detail::enable_if_module_t<T>>
|
| 161 |
+
T& get();
|
| 162 |
+
|
| 163 |
+
/// Attempts to cast the underlying module to the given module type. Throws an
|
| 164 |
+
/// exception if the types do not match.
|
| 165 |
+
template <typename T, typename = torch::detail::enable_if_module_t<T>>
|
| 166 |
+
const T& get() const;
|
| 167 |
+
|
| 168 |
+
/// Returns the contained module in a `nn::ModuleHolder` subclass if possible
|
| 169 |
+
/// (i.e. if `T` has a constructor for the underlying module type).
|
| 170 |
+
template <typename T, typename ContainedType = typename T::ContainedType>
|
| 171 |
+
T get() const;
|
| 172 |
+
|
| 173 |
+
/// Returns a `std::shared_ptr` whose dynamic type is that of the underlying
|
| 174 |
+
/// module.
|
| 175 |
+
std::shared_ptr<Module> ptr() const;
|
| 176 |
+
|
| 177 |
+
/// Like `ptr()`, but casts the pointer to the given type.
|
| 178 |
+
template <typename T, typename = torch::detail::enable_if_module_t<T>>
|
| 179 |
+
std::shared_ptr<T> ptr() const;
|
| 180 |
+
|
| 181 |
+
/// Returns the `type_info` object of the contained value.
|
| 182 |
+
const std::type_info& type_info() const;
|
| 183 |
+
|
| 184 |
+
/// Returns true if the `AnyModule` does not contain a module.
|
| 185 |
+
bool is_empty() const noexcept;
|
| 186 |
+
|
| 187 |
+
private:
|
| 188 |
+
/// Creates a `unique_ptr<AnyModulePlaceholder>` pointing to a
|
| 189 |
+
/// `AnyModuleHolder` of the correct type. This method is used to deduce the
|
| 190 |
+
/// arguments of the module's `forward()` method.
|
| 191 |
+
template <
|
| 192 |
+
typename ModuleType,
|
| 193 |
+
typename Class,
|
| 194 |
+
typename ReturnType,
|
| 195 |
+
typename... ArgumentTypes>
|
| 196 |
+
std::unique_ptr<AnyModulePlaceholder> make_holder(
|
| 197 |
+
std::shared_ptr<ModuleType>&& module,
|
| 198 |
+
ReturnType (Class::*)(ArgumentTypes...));
|
| 199 |
+
|
| 200 |
+
/// Helper method invoked by const and non-const `get()`.
|
| 201 |
+
template <typename ModuleType, typename ReturnType, typename... ArgumentTypes>
|
| 202 |
+
ModuleType& get_(ReturnType (ModuleType::*)(ArgumentTypes...)) const;
|
| 203 |
+
|
| 204 |
+
/// Helper method invoked by const and non-const `get()`.
|
| 205 |
+
template <typename ModuleType>
|
| 206 |
+
ModuleType& get_() const;
|
| 207 |
+
|
| 208 |
+
/// The type erased module.
|
| 209 |
+
std::unique_ptr<AnyModulePlaceholder> content_;
|
| 210 |
+
};
|
| 211 |
+
|
| 212 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AnyModule ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 213 |
+
|
| 214 |
+
template <typename ModuleType>
|
| 215 |
+
AnyModule::AnyModule(std::shared_ptr<ModuleType> module)
|
| 216 |
+
: content_(make_holder(
|
| 217 |
+
std::move(module),
|
| 218 |
+
&std::remove_reference<ModuleType>::type::forward)) {
|
| 219 |
+
// `AnyModule` can only store an `nn::Module` subclass object that provides
|
| 220 |
+
// a `forward()` method that has a non-templatized return type.
|
| 221 |
+
// (e.g. `AnyModule` cannot store `nn::Sequential`, because `nn::Sequential`'s
|
| 222 |
+
// `forward()` method has a templatized return type.)
|
| 223 |
+
static_assert(
|
| 224 |
+
torch::detail::is_module<ModuleType>::value,
|
| 225 |
+
"Can only store object derived from nn::Module into AnyModule");
|
| 226 |
+
static_assert(
|
| 227 |
+
torch::detail::has_forward<ModuleType>::value,
|
| 228 |
+
"Can only store module with a forward() method that has a non-templatized"
|
| 229 |
+
" argument type and return type into AnyModule (e.g. we cannot store nn::Sequential"
|
| 230 |
+
"into AnyModule, because its forward() method's argument type and return type are templatized."
|
| 231 |
+
" If you need to use nn::Sequentials inside each other you can subclass "
|
| 232 |
+
"nn::Sequential and write a non-templatized forward function for it. You can checkout "
|
| 233 |
+
"https://github.com/pytorch/vision/blob/2f46070f3cb1ea894d82578f3dc5677f82f34958/torchvision/csrc/models/mnasnet.cpp#L59 "
|
| 234 |
+
"for an example on how to do this.).");
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
template <typename ModuleType, typename>
|
| 238 |
+
AnyModule::AnyModule(ModuleType&& module)
|
| 239 |
+
: AnyModule(
|
| 240 |
+
std::make_shared<ModuleType>(std::forward<ModuleType>(module))) {}
|
| 241 |
+
|
| 242 |
+
template <typename ModuleType>
|
| 243 |
+
AnyModule::AnyModule(const ModuleHolder<ModuleType>& module_holder)
|
| 244 |
+
: AnyModule(module_holder.ptr()) {}
|
| 245 |
+
|
| 246 |
+
inline AnyModule::AnyModule(const AnyModule& other)
|
| 247 |
+
: content_(other.content_ ? other.content_->copy() : nullptr) {}
|
| 248 |
+
|
| 249 |
+
inline AnyModule& AnyModule::operator=(const AnyModule& other) {
|
| 250 |
+
if (this != &other) {
|
| 251 |
+
content_ = other.content_ ? other.content_->copy() : nullptr;
|
| 252 |
+
}
|
| 253 |
+
return *this;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
inline AnyModule AnyModule::clone(optional<Device> device) const {
|
| 257 |
+
AnyModule clone;
|
| 258 |
+
clone.content_ = content_ ? content_->clone_module(device) : nullptr;
|
| 259 |
+
return clone;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
template <typename ModuleType>
|
| 263 |
+
AnyModule& AnyModule::operator=(std::shared_ptr<ModuleType> module) {
|
| 264 |
+
// NOLINTNEXTLINE(cppcoreguidelines-c-copy-assignment-signature)
|
| 265 |
+
return (*this = AnyModule(std::move(module)));
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
template <typename... ArgumentTypes>
|
| 269 |
+
AnyValue AnyModule::any_forward(ArgumentTypes&&... arguments) {
|
| 270 |
+
TORCH_CHECK(!is_empty(), "Cannot call forward() on an empty AnyModule");
|
| 271 |
+
std::vector<AnyValue> values;
|
| 272 |
+
values.reserve(sizeof...(ArgumentTypes));
|
| 273 |
+
torch::apply(
|
| 274 |
+
[&values](AnyValue&& value) { values.push_back(std::move(value)); },
|
| 275 |
+
AnyValue(std::forward<ArgumentTypes>(arguments))...);
|
| 276 |
+
return content_->forward(std::move(values));
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
template <typename ReturnType, typename... ArgumentTypes>
|
| 280 |
+
ReturnType AnyModule::forward(ArgumentTypes&&... arguments) {
|
| 281 |
+
return any_forward(std::forward<ArgumentTypes>(arguments)...)
|
| 282 |
+
.template get<ReturnType>();
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
template <typename T, typename>
|
| 286 |
+
T& AnyModule::get() {
|
| 287 |
+
TORCH_CHECK(!is_empty(), "Cannot call get() on an empty AnyModule");
|
| 288 |
+
return get_<T>();
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
template <typename T, typename>
|
| 292 |
+
const T& AnyModule::get() const {
|
| 293 |
+
TORCH_CHECK(!is_empty(), "Cannot call get() on an empty AnyModule");
|
| 294 |
+
return get_<T>();
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
template <typename T, typename ContainedType>
|
| 298 |
+
T AnyModule::get() const {
|
| 299 |
+
return T(ptr<ContainedType>());
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
inline std::shared_ptr<Module> AnyModule::ptr() const {
|
| 303 |
+
TORCH_CHECK(!is_empty(), "Cannot call ptr() on an empty AnyModule");
|
| 304 |
+
return content_->ptr();
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
template <typename T, typename>
|
| 308 |
+
std::shared_ptr<T> AnyModule::ptr() const {
|
| 309 |
+
TORCH_CHECK(!is_empty(), "Cannot call ptr() on an empty AnyModule");
|
| 310 |
+
// Call get() but discard the value, just to do the type checking.
|
| 311 |
+
get_<T>();
|
| 312 |
+
return std::dynamic_pointer_cast<T>(ptr());
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
inline const std::type_info& AnyModule::type_info() const {
|
| 316 |
+
TORCH_CHECK(!is_empty(), "Cannot call type_info() on an empty AnyModule");
|
| 317 |
+
return content_->type_info;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
inline bool AnyModule::is_empty() const noexcept {
|
| 321 |
+
return content_ == nullptr;
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
// Private Methods
|
| 325 |
+
|
| 326 |
+
template <
|
| 327 |
+
typename ModuleType,
|
| 328 |
+
typename Class,
|
| 329 |
+
typename ReturnType,
|
| 330 |
+
typename... ArgumentTypes>
|
| 331 |
+
std::unique_ptr<AnyModulePlaceholder> AnyModule::make_holder(
|
| 332 |
+
std::shared_ptr<ModuleType>&& module,
|
| 333 |
+
ReturnType (Class::*)(ArgumentTypes...)) {
|
| 334 |
+
static_assert(
|
| 335 |
+
torch::detail::check_not_lvalue_references<ArgumentTypes...>(),
|
| 336 |
+
"Modules stored inside AnyModule must not take references. "
|
| 337 |
+
"Use pointers instead.");
|
| 338 |
+
static_assert(
|
| 339 |
+
!std::is_void<ReturnType>::value,
|
| 340 |
+
"AnyModule cannot store modules that return void "
|
| 341 |
+
"(you can return a dummy value).");
|
| 342 |
+
return std::make_unique<
|
| 343 |
+
AnyModuleHolder<decay_t<ModuleType>, ArgumentTypes...>>(
|
| 344 |
+
std::move(module));
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
template <typename ModuleType>
|
| 348 |
+
ModuleType& AnyModule::get_() const {
|
| 349 |
+
using M = typename std::remove_reference<ModuleType>::type;
|
| 350 |
+
static_assert(
|
| 351 |
+
torch::detail::has_forward<M>::value,
|
| 352 |
+
"Can only call AnyModule::get<T> with a type T that has a forward method");
|
| 353 |
+
return get_(&M::forward);
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
template <typename ModuleType, typename ReturnType, typename... ArgumentTypes>
|
| 357 |
+
ModuleType& AnyModule::get_(
|
| 358 |
+
ReturnType (ModuleType::*)(ArgumentTypes...)) const {
|
| 359 |
+
if (typeid(ModuleType).hash_code() == type_info().hash_code()) {
|
| 360 |
+
return *static_cast<AnyModuleHolder<ModuleType, ArgumentTypes...>&>(
|
| 361 |
+
*content_)
|
| 362 |
+
.module;
|
| 363 |
+
}
|
| 364 |
+
AT_ERROR(
|
| 365 |
+
"Attempted to cast module of type ",
|
| 366 |
+
c10::demangle(type_info().name()),
|
| 367 |
+
" to type ",
|
| 368 |
+
c10::demangle(typeid(ModuleType).name()));
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
} // namespace nn
|
| 372 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/any_module_holder.h
ADDED
|
@@ -0,0 +1,133 @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11 |
+
|
| 12 |
+
/// The static type of the object we store in the `AnyModule`, which erases
|
| 13 |
+
/// the actual type, but allows us to call `forward()` on the underlying
|
| 14 |
+
/// module.
|
| 15 |
+
struct AnyModulePlaceholder : public AnyValue::Placeholder {
|
| 16 |
+
using AnyValue::Placeholder::Placeholder;
|
| 17 |
+
|
| 18 |
+
/// The "erased" `forward()` method.
|
| 19 |
+
virtual AnyValue forward(std::vector<AnyValue>&& arguments) = 0;
|
| 20 |
+
|
| 21 |
+
/// Returns std::shared_ptr<Module> pointing to the erased module.
|
| 22 |
+
virtual std::shared_ptr<Module> ptr() = 0;
|
| 23 |
+
|
| 24 |
+
/// Returns a `AnyModulePlaceholder` with a shallow copy of this `AnyModule`.
|
| 25 |
+
virtual std::unique_ptr<AnyModulePlaceholder> copy() const = 0;
|
| 26 |
+
|
| 27 |
+
/// Returns a `AnyModulePlaceholder` with a deep copy of this `AnyModule`.
|
| 28 |
+
virtual std::unique_ptr<AnyModulePlaceholder> clone_module(
|
| 29 |
+
optional<Device> device) const = 0;
|
| 30 |
+
};
|
| 31 |
+
|
| 32 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AnyModuleHolder ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 33 |
+
|
| 34 |
+
/// The dynamic type of the object stored in the `AnyModule`. It contains the
|
| 35 |
+
/// concrete instance to which all calls are forwarded. It is parameterized
|
| 36 |
+
/// over the concrete type of the module, and the types of the arguments the
|
| 37 |
+
/// module takes in its `forward()` method.
|
| 38 |
+
template <typename ModuleType, typename... ArgumentTypes>
|
| 39 |
+
struct AnyModuleHolder : public AnyModulePlaceholder {
|
| 40 |
+
/// \internal
|
| 41 |
+
struct CheckedGetter {
|
| 42 |
+
template <typename T>
|
| 43 |
+
decay_t<T>&& operator()(size_t index) {
|
| 44 |
+
AT_ASSERT(index < arguments_.size());
|
| 45 |
+
auto& value = arguments_[index];
|
| 46 |
+
if (auto* maybe_value = value.template try_get<decay_t<T>>()) {
|
| 47 |
+
return std::move(*maybe_value);
|
| 48 |
+
}
|
| 49 |
+
AT_ERROR(
|
| 50 |
+
"Expected argument #",
|
| 51 |
+
index,
|
| 52 |
+
" to be of type ",
|
| 53 |
+
c10::demangle(typeid(T).name()),
|
| 54 |
+
", but received value of type ",
|
| 55 |
+
c10::demangle(value.type_info().name()));
|
| 56 |
+
}
|
| 57 |
+
std::vector<AnyValue>& arguments_;
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
/// \internal
|
| 61 |
+
struct InvokeForward {
|
| 62 |
+
template <typename... Ts>
|
| 63 |
+
AnyValue operator()(Ts&&... ts) {
|
| 64 |
+
return AnyValue(module_->forward(std::forward<Ts>(ts)...));
|
| 65 |
+
}
|
| 66 |
+
std::shared_ptr<ModuleType>& module_;
|
| 67 |
+
};
|
| 68 |
+
|
| 69 |
+
/// Constructs the `AnyModuleHolder` from a concrete module.
|
| 70 |
+
explicit AnyModuleHolder(std::shared_ptr<ModuleType>&& module_)
|
| 71 |
+
: AnyModulePlaceholder(typeid(ModuleType)), module(std::move(module_)) {}
|
| 72 |
+
|
| 73 |
+
/// Calls `forward()` on the underlying module, casting each `AnyValue` in the
|
| 74 |
+
/// argument vector to a concrete value.
|
| 75 |
+
AnyValue forward(std::vector<AnyValue>&& arguments) override {
|
| 76 |
+
if (module->_forward_has_default_args()) {
|
| 77 |
+
TORCH_CHECK(
|
| 78 |
+
arguments.size() >= module->_forward_num_required_args() &&
|
| 79 |
+
arguments.size() <= sizeof...(ArgumentTypes),
|
| 80 |
+
c10::demangle(type_info.name()),
|
| 81 |
+
"'s forward() method expects at least ",
|
| 82 |
+
module->_forward_num_required_args(),
|
| 83 |
+
" argument(s) and at most ",
|
| 84 |
+
sizeof...(ArgumentTypes),
|
| 85 |
+
" argument(s), but received ",
|
| 86 |
+
arguments.size(),
|
| 87 |
+
".");
|
| 88 |
+
arguments = std::move(
|
| 89 |
+
module->_forward_populate_default_args(std::move(arguments)));
|
| 90 |
+
} else {
|
| 91 |
+
std::string use_default_args_macro_prompt = " If " +
|
| 92 |
+
c10::demangle(type_info.name()) +
|
| 93 |
+
"'s forward() method has default arguments, " +
|
| 94 |
+
"please make sure the forward() method is declared with a corresponding `FORWARD_HAS_DEFAULT_ARGS` macro.";
|
| 95 |
+
TORCH_CHECK(
|
| 96 |
+
arguments.size() == sizeof...(ArgumentTypes),
|
| 97 |
+
c10::demangle(type_info.name()),
|
| 98 |
+
"'s forward() method expects ",
|
| 99 |
+
sizeof...(ArgumentTypes),
|
| 100 |
+
" argument(s), but received ",
|
| 101 |
+
arguments.size(),
|
| 102 |
+
".",
|
| 103 |
+
(arguments.size() < sizeof...(ArgumentTypes))
|
| 104 |
+
? use_default_args_macro_prompt
|
| 105 |
+
: "");
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// FYI: During invocation of a module's `forward()` method, the values live
|
| 109 |
+
// in the `arguments` vector inside this function.
|
| 110 |
+
return torch::unpack<AnyValue, ArgumentTypes...>(
|
| 111 |
+
InvokeForward{module}, CheckedGetter{arguments});
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
std::shared_ptr<Module> ptr() override {
|
| 115 |
+
return module;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
std::unique_ptr<AnyModulePlaceholder> copy() const override {
|
| 119 |
+
return std::make_unique<AnyModuleHolder>(*this);
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
std::unique_ptr<AnyModulePlaceholder> clone_module(
|
| 123 |
+
optional<Device> device) const override {
|
| 124 |
+
return std::make_unique<AnyModuleHolder>(
|
| 125 |
+
std::dynamic_pointer_cast<ModuleType>(module->clone(device)));
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
/// The actual concrete module instance.
|
| 129 |
+
std::shared_ptr<ModuleType> module;
|
| 130 |
+
};
|
| 131 |
+
|
| 132 |
+
} // namespace nn
|
| 133 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/any_value.h
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/detail/static.h>
|
| 4 |
+
#include <torch/nn/module.h>
|
| 5 |
+
#include <torch/nn/pimpl.h>
|
| 6 |
+
#include <torch/types.h>
|
| 7 |
+
|
| 8 |
+
#include <torch/csrc/autograd/variable.h>
|
| 9 |
+
#include <torch/csrc/utils/variadic.h>
|
| 10 |
+
|
| 11 |
+
#include <memory>
|
| 12 |
+
#include <type_traits>
|
| 13 |
+
#include <typeinfo>
|
| 14 |
+
#include <utility>
|
| 15 |
+
|
| 16 |
+
namespace torch {
|
| 17 |
+
namespace nn {
|
| 18 |
+
|
| 19 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AnyValue ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 20 |
+
|
| 21 |
+
/// An implementation of `std::any` which stores
|
| 22 |
+
/// a type erased object, whose concrete value can be retrieved at runtime by
|
| 23 |
+
/// checking if the `typeid()` of a requested type matches the `typeid()` of
|
| 24 |
+
/// the object stored.
|
| 25 |
+
class AnyValue {
|
| 26 |
+
public:
|
| 27 |
+
/// Move construction and assignment is allowed, and follows the default
|
| 28 |
+
/// behavior of move for `std::unique_ptr`.
|
| 29 |
+
AnyValue(AnyValue&&) = default;
|
| 30 |
+
AnyValue& operator=(AnyValue&&) = default;
|
| 31 |
+
|
| 32 |
+
/// Copy construction and assignment is allowed.
|
| 33 |
+
AnyValue(const AnyValue& other) : content_(other.content_->clone()) {}
|
| 34 |
+
AnyValue& operator=(const AnyValue& other) {
|
| 35 |
+
content_ = other.content_->clone();
|
| 36 |
+
return *this;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/// Constructs the `AnyValue` from value type.
|
| 40 |
+
template <typename T>
|
| 41 |
+
// NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
|
| 42 |
+
explicit AnyValue(T&& value)
|
| 43 |
+
: content_(std::make_unique<Holder<decay_t<T>>>(std::forward<T>(value))) {
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/// Returns a pointer to the value contained in the `AnyValue` if the type
|
| 47 |
+
/// passed as template parameter matches the type of the value stored, and
|
| 48 |
+
/// returns a null pointer otherwise.
|
| 49 |
+
template <typename T>
|
| 50 |
+
T* try_get() {
|
| 51 |
+
static_assert(
|
| 52 |
+
!std::is_reference<T>::value,
|
| 53 |
+
"AnyValue stores decayed types, you cannot cast it to a reference type");
|
| 54 |
+
static_assert(
|
| 55 |
+
!std::is_array<T>::value,
|
| 56 |
+
"AnyValue stores decayed types, you must cast it to T* instead of T[]");
|
| 57 |
+
if (typeid(T).hash_code() == type_info().hash_code()) {
|
| 58 |
+
return &static_cast<Holder<T>&>(*content_).value;
|
| 59 |
+
}
|
| 60 |
+
return nullptr;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/// Returns the value contained in the `AnyValue` if the type passed as
|
| 64 |
+
/// template parameter matches the type of the value stored, and throws an
|
| 65 |
+
/// exception otherwise.
|
| 66 |
+
template <typename T>
|
| 67 |
+
T get() {
|
| 68 |
+
if (auto* maybe_value = try_get<T>()) {
|
| 69 |
+
return *maybe_value;
|
| 70 |
+
}
|
| 71 |
+
AT_ERROR(
|
| 72 |
+
"Attempted to cast AnyValue to ",
|
| 73 |
+
c10::demangle(typeid(T).name()),
|
| 74 |
+
", but its actual type is ",
|
| 75 |
+
c10::demangle(type_info().name()));
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/// Returns the `type_info` object of the contained value.
|
| 79 |
+
const std::type_info& type_info() const noexcept {
|
| 80 |
+
return content_->type_info;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
private:
|
| 84 |
+
friend struct AnyModulePlaceholder;
|
| 85 |
+
friend struct TestAnyValue;
|
| 86 |
+
|
| 87 |
+
/// \internal
|
| 88 |
+
/// The static type of the object we store in the `AnyValue`, which erases the
|
| 89 |
+
/// actual object's type, allowing us only to check the `type_info` of the
|
| 90 |
+
/// type stored in the dynamic type.
|
| 91 |
+
struct Placeholder {
|
| 92 |
+
explicit Placeholder(const std::type_info& type_info_) noexcept
|
| 93 |
+
: type_info(type_info_) {}
|
| 94 |
+
Placeholder(const Placeholder&) = default;
|
| 95 |
+
Placeholder(Placeholder&&) = default;
|
| 96 |
+
virtual ~Placeholder() = default;
|
| 97 |
+
virtual std::unique_ptr<Placeholder> clone() const {
|
| 98 |
+
TORCH_CHECK(false, "clone() should only be called on `AnyValue::Holder`");
|
| 99 |
+
}
|
| 100 |
+
const std::type_info& type_info;
|
| 101 |
+
};
|
| 102 |
+
|
| 103 |
+
/// \internal
|
| 104 |
+
/// The dynamic type of the object we store in the `AnyValue`, which hides the
|
| 105 |
+
/// actual object we have erased in this `AnyValue`.
|
| 106 |
+
template <typename T>
|
| 107 |
+
struct Holder : public Placeholder {
|
| 108 |
+
/// A template because T&& would not be universal reference here.
|
| 109 |
+
template <typename U>
|
| 110 |
+
// NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
|
| 111 |
+
explicit Holder(U&& value_) noexcept
|
| 112 |
+
: Placeholder(typeid(T)), value(std::forward<U>(value_)) {}
|
| 113 |
+
std::unique_ptr<Placeholder> clone() const override {
|
| 114 |
+
return std::make_unique<Holder<T>>(value);
|
| 115 |
+
}
|
| 116 |
+
T value;
|
| 117 |
+
};
|
| 118 |
+
|
| 119 |
+
/// The type erased object.
|
| 120 |
+
std::unique_ptr<Placeholder> content_;
|
| 121 |
+
};
|
| 122 |
+
|
| 123 |
+
} // namespace nn
|
| 124 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/functional.h
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/csrc/Export.h>
|
| 4 |
+
#include <torch/csrc/utils/variadic.h>
|
| 5 |
+
#include <torch/nn/cloneable.h>
|
| 6 |
+
#include <torch/nn/pimpl.h>
|
| 7 |
+
#include <torch/types.h>
|
| 8 |
+
|
| 9 |
+
#include <functional>
|
| 10 |
+
#include <utility>
|
| 11 |
+
|
| 12 |
+
namespace torch {
|
| 13 |
+
namespace nn {
|
| 14 |
+
|
| 15 |
+
/// Wraps a function in a `Module`.
|
| 16 |
+
///
|
| 17 |
+
/// The `Functional` module allows wrapping an arbitrary function or function
|
| 18 |
+
/// object in an `nn::Module`. This is primarily handy for usage in
|
| 19 |
+
/// `Sequential`.
|
| 20 |
+
///
|
| 21 |
+
/// \rst
|
| 22 |
+
/// .. code-block:: cpp
|
| 23 |
+
///
|
| 24 |
+
/// Sequential sequential(
|
| 25 |
+
/// Linear(3, 4),
|
| 26 |
+
/// Functional(torch::relu),
|
| 27 |
+
/// BatchNorm1d(3),
|
| 28 |
+
/// Functional(torch::elu, /*alpha=*/1));
|
| 29 |
+
/// \endrst
|
| 30 |
+
///
|
| 31 |
+
/// While a `Functional` module only accepts a single `Tensor` as input, it is
|
| 32 |
+
/// possible for the wrapped function to accept further arguments. However,
|
| 33 |
+
/// these have to be bound *at construction time*. For example, if
|
| 34 |
+
/// you want to wrap `torch::leaky_relu`, which accepts a `slope` scalar as its
|
| 35 |
+
/// second argument, with a particular value for its `slope` in a `Functional`
|
| 36 |
+
/// module, you could write
|
| 37 |
+
///
|
| 38 |
+
/// \rst
|
| 39 |
+
/// .. code-block:: cpp
|
| 40 |
+
///
|
| 41 |
+
/// Functional(torch::leaky_relu, /*slope=*/0.5)
|
| 42 |
+
/// \endrst
|
| 43 |
+
///
|
| 44 |
+
/// The value of `0.5` is then stored within the `Functional` object and
|
| 45 |
+
/// supplied to the function call at invocation time. Note that such bound
|
| 46 |
+
/// values are evaluated eagerly and stored a single time. See the documentation
|
| 47 |
+
/// of [std::bind](https://en.cppreference.com/w/cpp/utility/functional/bind)
|
| 48 |
+
/// for more information on the semantics of argument binding.
|
| 49 |
+
///
|
| 50 |
+
/// \rst
|
| 51 |
+
/// .. attention::
|
| 52 |
+
/// After passing any bound arguments, the function must accept a single
|
| 53 |
+
/// tensor and return a single tensor.
|
| 54 |
+
/// \endrst
|
| 55 |
+
///
|
| 56 |
+
/// Note that `Functional` overloads the call operator (`operator()`) such that
|
| 57 |
+
/// you can invoke it with `my_func(...)`.
|
| 58 |
+
class TORCH_API FunctionalImpl : public torch::nn::Cloneable<FunctionalImpl> {
|
| 59 |
+
public:
|
| 60 |
+
using Function = std::function<Tensor(Tensor)>;
|
| 61 |
+
|
| 62 |
+
/// Constructs a `Functional` from a function object.
|
| 63 |
+
explicit FunctionalImpl(Function function);
|
| 64 |
+
|
| 65 |
+
template <
|
| 66 |
+
typename SomeFunction,
|
| 67 |
+
typename... Args,
|
| 68 |
+
typename = torch::enable_if_t<(sizeof...(Args) > 0)>>
|
| 69 |
+
explicit FunctionalImpl(SomeFunction original_function, Args&&... args)
|
| 70 |
+
// NOLINTNEXTLINE(modernize-avoid-bind)
|
| 71 |
+
: function_(std::bind(
|
| 72 |
+
original_function,
|
| 73 |
+
/*input=*/std::placeholders::_1,
|
| 74 |
+
std::forward<Args>(args)...)) {
|
| 75 |
+
// std::bind is normally evil, but (1) gcc is broken w.r.t. handling
|
| 76 |
+
// parameter pack expansion in lambdas and (2) moving parameter packs into
|
| 77 |
+
// a lambda only works with C++14, so std::bind is the more move-aware
|
| 78 |
+
// solution here.
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
void reset() override;
|
| 82 |
+
|
| 83 |
+
/// Pretty prints the `Functional` module into the given `stream`.
|
| 84 |
+
void pretty_print(std::ostream& stream) const override;
|
| 85 |
+
|
| 86 |
+
/// Forwards the `input` tensor to the underlying (bound) function object.
|
| 87 |
+
Tensor forward(Tensor input);
|
| 88 |
+
|
| 89 |
+
/// Calls forward(input).
|
| 90 |
+
Tensor operator()(Tensor input);
|
| 91 |
+
|
| 92 |
+
bool is_serializable() const override;
|
| 93 |
+
|
| 94 |
+
private:
|
| 95 |
+
Function function_;
|
| 96 |
+
};
|
| 97 |
+
|
| 98 |
+
/// A `ModuleHolder` subclass for `FunctionalImpl`.
|
| 99 |
+
/// See the documentation for `FunctionalImpl` class to learn what methods it
|
| 100 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 101 |
+
/// module storage semantics.
|
| 102 |
+
TORCH_MODULE(Functional);
|
| 103 |
+
|
| 104 |
+
} // namespace nn
|
| 105 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/moduledict.h
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/cloneable.h>
|
| 4 |
+
#include <torch/nn/module.h>
|
| 5 |
+
#include <torch/ordered_dict.h>
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
namespace torch {
|
| 9 |
+
namespace nn {
|
| 10 |
+
|
| 11 |
+
/// An OrderedDict of `Module`s that registers its elements by their `key`s.
|
| 12 |
+
///
|
| 13 |
+
/// \rst
|
| 14 |
+
/// .. code-block:: cpp
|
| 15 |
+
///
|
| 16 |
+
/// torch::OrderedDict<std::string, std::shared_ptr<Module>> ordereddict = {
|
| 17 |
+
/// {"linear", Linear(10, 3).ptr()},
|
| 18 |
+
/// {"conv", Conv2d(1, 2, 3).ptr()},
|
| 19 |
+
/// {"dropout", Dropout(0.5).ptr()},
|
| 20 |
+
/// };
|
| 21 |
+
/// torch::nn::ModuleDict dict1(ordereddict);
|
| 22 |
+
///
|
| 23 |
+
/// for (const auto &module : *dict1) {
|
| 24 |
+
/// module->pretty_print(std::cout);
|
| 25 |
+
/// }
|
| 26 |
+
///
|
| 27 |
+
/// std::vector<std::pair<std::string, std::shared_ptr<Module>>> list = {
|
| 28 |
+
/// {"linear", Linear(10, 3).ptr()},
|
| 29 |
+
/// {"conv", Conv2d(1, 2, 3).ptr()},
|
| 30 |
+
/// {"dropout", Dropout(0.5).ptr()},
|
| 31 |
+
/// };
|
| 32 |
+
/// torch::nn::ModuleDict dict2(list);
|
| 33 |
+
///
|
| 34 |
+
/// for (const auto &module : *dict2) {
|
| 35 |
+
/// module->pretty_print(std::cout);
|
| 36 |
+
/// }
|
| 37 |
+
///
|
| 38 |
+
/// \endrst
|
| 39 |
+
///
|
| 40 |
+
/// Why should you use `ModuleDict` instead of a simple `map` or `OrderedDict`?
|
| 41 |
+
/// The value a `ModuleDict` provides over manually calling an ordered map of
|
| 42 |
+
/// modules is that it allows treating the whole container *as a single module*,
|
| 43 |
+
/// such that performing a transformation on the `ModuleDict` applies to each of
|
| 44 |
+
/// the modules it stores (which are each a registered submodule of the
|
| 45 |
+
/// `ModuleDict`). For example, calling `.to(torch::kCUDA)` on a `ModuleDict`
|
| 46 |
+
/// will move each module in the map to CUDA memory. For example:
|
| 47 |
+
///
|
| 48 |
+
/// \rst
|
| 49 |
+
/// .. code-block:: cpp
|
| 50 |
+
///
|
| 51 |
+
/// torch::OrderedDict<std::string, std::shared_ptr<Module>> ordereddict = {
|
| 52 |
+
/// {"linear", Linear(10, 3).ptr()},
|
| 53 |
+
/// {"conv", Conv2d(1, 2, 3).ptr()},
|
| 54 |
+
/// {"dropout", Dropout(0.5).ptr()},
|
| 55 |
+
/// };
|
| 56 |
+
/// torch::nn::ModuleDict dict(ordereddict);
|
| 57 |
+
///
|
| 58 |
+
/// // Convert all modules to CUDA.
|
| 59 |
+
/// dict->to(torch::kCUDA);
|
| 60 |
+
///
|
| 61 |
+
/// \endrst
|
| 62 |
+
///
|
| 63 |
+
/// Finally, `ModuleDict` provides a lightweight container API, such as allowing
|
| 64 |
+
/// iteration over submodules, positional access, adding new modules from a
|
| 65 |
+
/// vector of key-module pairs or an `OrderedDict` or another `ModuleDict` after
|
| 66 |
+
/// construction via `update`.
|
| 67 |
+
class ModuleDictImpl : public Cloneable<ModuleDictImpl> {
|
| 68 |
+
public:
|
| 69 |
+
using Iterator =
|
| 70 |
+
torch::OrderedDict<std::string, std::shared_ptr<Module>>::Iterator;
|
| 71 |
+
using ConstIterator =
|
| 72 |
+
torch::OrderedDict<std::string, std::shared_ptr<Module>>::ConstIterator;
|
| 73 |
+
|
| 74 |
+
ModuleDictImpl() = default;
|
| 75 |
+
|
| 76 |
+
/// Constructs the `ModuleDict` from a list of string-Module pairs.
|
| 77 |
+
explicit ModuleDictImpl(
|
| 78 |
+
const std::vector<std::pair<std::string, std::shared_ptr<Module>>>&
|
| 79 |
+
modules) {
|
| 80 |
+
update(modules);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/// Constructs the `ModuleDict` from an `OrderedDict`.
|
| 84 |
+
explicit ModuleDictImpl(
|
| 85 |
+
const torch::OrderedDict<std::string, std::shared_ptr<Module>>& modules) {
|
| 86 |
+
update(modules);
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/// Return the items in the `ModuleDict`.
|
| 90 |
+
std::vector<std::pair<std::string, std::shared_ptr<Module>>> items() const {
|
| 91 |
+
return modules_.pairs();
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
/// Return the keys in the `ModuleDict`.
|
| 95 |
+
std::vector<std::string> keys() const {
|
| 96 |
+
return modules_.keys();
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
/// Return the values in the `ModuleDict`.
|
| 100 |
+
std::vector<std::shared_ptr<Module>> values() const {
|
| 101 |
+
return modules_.values();
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
/// Return an iterator to the start of `ModuleDict`.
|
| 105 |
+
Iterator begin() {
|
| 106 |
+
return modules_.begin();
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
/// Return a const iterator to the start of `ModuleDict`.
|
| 110 |
+
ConstIterator begin() const {
|
| 111 |
+
return modules_.begin();
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
/// Return an iterator to the end of `ModuleDict`.
|
| 115 |
+
Iterator end() {
|
| 116 |
+
return modules_.end();
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/// Return a const iterator to the end of `ModuleDict`.
|
| 120 |
+
ConstIterator end() const {
|
| 121 |
+
return modules_.end();
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
/// Return the number of items currently stored in the `ModuleDict`.
|
| 125 |
+
size_t size() const noexcept {
|
| 126 |
+
return modules_.size();
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
/// Return true if the `ModuleDict` is empty, otherwise return false.
|
| 130 |
+
bool empty() const noexcept {
|
| 131 |
+
return modules_.is_empty();
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
/// Check if the centain parameter with the key in the `ModuleDict`.
|
| 135 |
+
bool contains(const std::string& key) const noexcept {
|
| 136 |
+
return modules_.contains(key);
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
/// Remove all items from the `ModuleDict`.
|
| 140 |
+
void clear() {
|
| 141 |
+
// Not remove the registration of modules to make it consistent with python
|
| 142 |
+
// version.
|
| 143 |
+
modules_.clear();
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
/// Special cloning function for `ModuleDict` because it does not use
|
| 147 |
+
/// `reset()`.
|
| 148 |
+
std::shared_ptr<Module> clone(
|
| 149 |
+
const optional<Device>& device = nullopt) const override {
|
| 150 |
+
auto clone = std::make_shared<ModuleDictImpl>();
|
| 151 |
+
for (const auto& module : modules_) {
|
| 152 |
+
clone->insert(module.key(), module.value()->clone(device));
|
| 153 |
+
}
|
| 154 |
+
return clone;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
/// `reset()` is empty for `ModuleDict`, since it does not have parameters of
|
| 158 |
+
/// its own.
|
| 159 |
+
void reset() override {}
|
| 160 |
+
|
| 161 |
+
/// Pretty prints the `ModuleDict` into the given `stream`.
|
| 162 |
+
void pretty_print(std::ostream& stream) const override {
|
| 163 |
+
stream << "torch::nn::ModuleDict";
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
/// Attempts to returns the `Module` associated with the given `key`. Throws
|
| 167 |
+
/// an exception if no such `key` is stored in the `ModuleDict`. Check
|
| 168 |
+
/// contains(key) before for a non-throwing way of access.
|
| 169 |
+
std::shared_ptr<Module> operator[](const std::string& key) const {
|
| 170 |
+
return modules_[key];
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
/// Attempts to return the module at the given key as the requested type.
|
| 174 |
+
/// Throws an exception if no such `key` is stored in the `ModuleDict`.
|
| 175 |
+
/// Check contains(key) before for a non-throwing way of access.
|
| 176 |
+
template <typename T>
|
| 177 |
+
T& at(const std::string& key) {
|
| 178 |
+
static_assert(
|
| 179 |
+
torch::detail::is_module<T>::value,
|
| 180 |
+
"Can only call ModuleList::at with an nn::Module type");
|
| 181 |
+
auto module = modules_[key]->as<T>();
|
| 182 |
+
TORCH_CHECK(
|
| 183 |
+
module,
|
| 184 |
+
"Unable to cast module[",
|
| 185 |
+
key,
|
| 186 |
+
"] to ",
|
| 187 |
+
c10::demangle(typeid(T).name()));
|
| 188 |
+
return *module;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
/// Attempts to return the module at the given key as the requested type.
|
| 192 |
+
/// Throws an exception if no such `key` is stored in the `ModuleDict`.
|
| 193 |
+
/// Check contains(key) before for a non-throwing way of access.
|
| 194 |
+
template <typename T>
|
| 195 |
+
const T& at(const std::string& key) const {
|
| 196 |
+
static_assert(
|
| 197 |
+
torch::detail::is_module<T>::value,
|
| 198 |
+
"Can only call ModuleList::at with an nn::Module type");
|
| 199 |
+
const auto module = modules_[key]->as<T>();
|
| 200 |
+
TORCH_CHECK(
|
| 201 |
+
module,
|
| 202 |
+
"Unable to cast module[",
|
| 203 |
+
key,
|
| 204 |
+
"] to ",
|
| 205 |
+
c10::demangle(typeid(T).name()));
|
| 206 |
+
return *module;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
/// Removes and returns the `Module` associated with the given `key`.
|
| 210 |
+
/// Throws an exception if no such `key` is stored in the `ModuleDict`.
|
| 211 |
+
/// Check contains(key) before for a non-throwing way of access.
|
| 212 |
+
std::shared_ptr<Module> pop(const std::string& key) {
|
| 213 |
+
auto module = modules_[key];
|
| 214 |
+
modules_.erase(key);
|
| 215 |
+
// Not remove the registration of the module to make it consistent with
|
| 216 |
+
// python version.
|
| 217 |
+
return module;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
/// Updated the `ModuleDict` with a vector of key-module pairs.
|
| 221 |
+
void update(
|
| 222 |
+
const std::vector<std::pair<std::string, std::shared_ptr<Module>>>&
|
| 223 |
+
modules) {
|
| 224 |
+
for (auto& item : modules) {
|
| 225 |
+
insert(item.first, item.second);
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
/// Updated the `ModuleDict` with key-value pairs from `OrderedDict` or
|
| 230 |
+
/// `ModuleDict`.
|
| 231 |
+
template <typename Container>
|
| 232 |
+
void update(const Container& container) {
|
| 233 |
+
for (auto& item : container) {
|
| 234 |
+
insert(item.key(), item.value());
|
| 235 |
+
}
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
private:
|
| 239 |
+
/// Private `OrderedDict` holding the key-Module pairs.
|
| 240 |
+
torch::OrderedDict<std::string, std::shared_ptr<Module>> modules_;
|
| 241 |
+
|
| 242 |
+
/// Insert a key-module pair by overwriting existing keys,
|
| 243 |
+
/// and register or replace the `Module`.
|
| 244 |
+
void insert(const std::string& key, std::shared_ptr<Module> module) {
|
| 245 |
+
if (contains(key)) {
|
| 246 |
+
modules_[key] = std::move(module);
|
| 247 |
+
replace_module(key, modules_[key]);
|
| 248 |
+
} else {
|
| 249 |
+
modules_.insert(key, std::move(module));
|
| 250 |
+
register_module(key, modules_.back().value());
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
};
|
| 254 |
+
|
| 255 |
+
/// A `ModuleHolder` subclass for `ModuleDictImpl`.
|
| 256 |
+
/// See the documentation for `ModuleDictImpl` class to learn what methods it
|
| 257 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 258 |
+
/// module storage semantics.
|
| 259 |
+
TORCH_MODULE(ModuleDict);
|
| 260 |
+
|
| 261 |
+
} // namespace nn
|
| 262 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/modulelist.h
ADDED
|
@@ -0,0 +1,274 @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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <c10/util/irange.h>
|
| 4 |
+
#include <torch/nn/cloneable.h>
|
| 5 |
+
#include <torch/nn/module.h>
|
| 6 |
+
|
| 7 |
+
#include <utility>
|
| 8 |
+
#include <vector>
|
| 9 |
+
|
| 10 |
+
namespace torch {
|
| 11 |
+
namespace nn {
|
| 12 |
+
|
| 13 |
+
/// A list of `Module`s that registers its elements.
|
| 14 |
+
///
|
| 15 |
+
/// \rst
|
| 16 |
+
/// .. code-block:: cpp
|
| 17 |
+
///
|
| 18 |
+
/// torch::nn::ModuleList mlist(
|
| 19 |
+
/// torch::nn::Linear(3, 4),
|
| 20 |
+
/// torch::nn::BatchNorm1d(4),
|
| 21 |
+
/// torch::nn::Dropout(0.5)
|
| 22 |
+
/// );
|
| 23 |
+
///
|
| 24 |
+
/// for (const auto &module : *mlist) {
|
| 25 |
+
/// module->pretty_print(std::cout);
|
| 26 |
+
/// }
|
| 27 |
+
///
|
| 28 |
+
/// \endrst
|
| 29 |
+
///
|
| 30 |
+
/// Why should you use `ModuleList` instead of a simple `std::vector`? The value
|
| 31 |
+
/// a `ModuleList` provides over manually calling a sequence of modules is that
|
| 32 |
+
/// it allows treating the whole container *as a single module*, such that
|
| 33 |
+
/// performing a transformation on the `ModuleList` applies to each of the
|
| 34 |
+
/// modules it stores (which are each a registered submodule of the
|
| 35 |
+
/// `ModuleList`). For example, calling
|
| 36 |
+
/// `.to(torch::kCUDA)` on a `ModuleList` will move each module in the list to
|
| 37 |
+
/// CUDA memory. For example:
|
| 38 |
+
///
|
| 39 |
+
/// \rst
|
| 40 |
+
/// .. code-block:: cpp
|
| 41 |
+
///
|
| 42 |
+
/// torch::nn::ModuleList mlist(
|
| 43 |
+
/// torch::nn::Linear(3, 4),
|
| 44 |
+
/// torch::nn::BatchNorm1d(4),
|
| 45 |
+
/// torch::nn::Dropout(0.5)
|
| 46 |
+
/// );
|
| 47 |
+
///
|
| 48 |
+
/// // Convert all modules to CUDA.
|
| 49 |
+
/// mlist->to(torch::kCUDA);
|
| 50 |
+
///
|
| 51 |
+
/// \endrst
|
| 52 |
+
///
|
| 53 |
+
/// Finally, `ModuleList` provides a lightweight container API, such as allowing
|
| 54 |
+
/// iteration over submodules, positional access, adding a new module after
|
| 55 |
+
/// construction via `push_back`, as well as joining two `ModuleList`s via
|
| 56 |
+
/// `extend`.
|
| 57 |
+
class ModuleListImpl : public Cloneable<ModuleListImpl> {
|
| 58 |
+
public:
|
| 59 |
+
using Iterator = std::vector<std::shared_ptr<Module>>::iterator;
|
| 60 |
+
using ConstIterator = std::vector<std::shared_ptr<Module>>::const_iterator;
|
| 61 |
+
|
| 62 |
+
ModuleListImpl() = default;
|
| 63 |
+
|
| 64 |
+
/// Constructs the `ModuleList` from a variadic list of modules.
|
| 65 |
+
template <typename... Modules>
|
| 66 |
+
explicit ModuleListImpl(Modules&&... modules) {
|
| 67 |
+
modules_.reserve(sizeof...(Modules));
|
| 68 |
+
push_back_var(std::forward<Modules>(modules)...);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/// Special cloning function for `ModuleList` because it does not use
|
| 72 |
+
/// `reset()`.
|
| 73 |
+
std::shared_ptr<Module> clone(
|
| 74 |
+
const optional<Device>& device = nullopt) const override {
|
| 75 |
+
auto clone = std::make_shared<ModuleListImpl>();
|
| 76 |
+
for (const auto& module : modules_) {
|
| 77 |
+
clone->push_back(module->clone(device));
|
| 78 |
+
}
|
| 79 |
+
return clone;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
/// `reset()` is empty for `ModuleList`, since it does not have parameters of
|
| 83 |
+
/// its own.
|
| 84 |
+
void reset() override {}
|
| 85 |
+
|
| 86 |
+
/// Pretty prints the `ModuleList` module into the given `stream`.
|
| 87 |
+
void pretty_print(std::ostream& stream) const override {
|
| 88 |
+
stream << "torch::nn::ModuleList";
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
void push_back(std::shared_ptr<Module> module) {
|
| 92 |
+
modules_.push_back(std::move(module));
|
| 93 |
+
const auto index = modules_.size() - 1;
|
| 94 |
+
register_module(c10::to_string(index), modules_[index]);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
/// Adds a new `Module` to the `ModuleList` container, moving or copying
|
| 98 |
+
/// it into a `shared_ptr` internally. This method allows passing value types,
|
| 99 |
+
/// and letting the container deal with the boxing.
|
| 100 |
+
template <typename M, typename = torch::detail::enable_if_module_t<M>>
|
| 101 |
+
void push_back(M&& module) {
|
| 102 |
+
using Type = typename std::remove_reference<M>::type;
|
| 103 |
+
push_back(std::make_shared<Type>(std::forward<M>(module)));
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
/// Unwraps the contained module of a `ModuleHolder` and adds it to the
|
| 107 |
+
/// `ModuleList`.
|
| 108 |
+
template <typename M>
|
| 109 |
+
void push_back(const ModuleHolder<M>& module_holder) {
|
| 110 |
+
push_back(module_holder.ptr());
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/// Iterates over the container and calls `push_back()` on each value.
|
| 114 |
+
template <typename Container>
|
| 115 |
+
void extend(const Container& container) {
|
| 116 |
+
for (const auto& module : container) {
|
| 117 |
+
push_back(module);
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
/// Returns an iterator to the start of the `ModuleList`.
|
| 122 |
+
Iterator begin() {
|
| 123 |
+
return modules_.begin();
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
/// Returns a const iterator to the start of the `ModuleList`.
|
| 127 |
+
ConstIterator begin() const {
|
| 128 |
+
return modules_.begin();
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
/// Returns an iterator to the end of the `ModuleList`.
|
| 132 |
+
Iterator end() {
|
| 133 |
+
return modules_.end();
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/// Returns a const iterator to the end of the `ModuleList`.
|
| 137 |
+
ConstIterator end() const {
|
| 138 |
+
return modules_.end();
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
/// Attempts to return the module at the given index as the requested type.
|
| 142 |
+
/// Throws an exception if the index is out of bounds or the types do not
|
| 143 |
+
/// match.
|
| 144 |
+
template <typename T>
|
| 145 |
+
T& at(size_t index) {
|
| 146 |
+
static_assert(
|
| 147 |
+
torch::detail::is_module<T>::value,
|
| 148 |
+
"Can only call ModuleList::at with an nn::Module type");
|
| 149 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 150 |
+
auto module = modules_[index]->as<T>();
|
| 151 |
+
TORCH_CHECK(
|
| 152 |
+
module,
|
| 153 |
+
"Unable to cast module[",
|
| 154 |
+
index,
|
| 155 |
+
"] to ",
|
| 156 |
+
c10::demangle(typeid(T).name()));
|
| 157 |
+
return *module;
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
/// Attempts to return the module at the given index as the requested type.
|
| 161 |
+
/// Throws an exception if the index is out of bounds or the types do not
|
| 162 |
+
/// match.
|
| 163 |
+
template <typename T>
|
| 164 |
+
const T& at(size_t index) const {
|
| 165 |
+
static_assert(
|
| 166 |
+
torch::detail::is_module<T>::value,
|
| 167 |
+
"Can only call ModuleList::at with an nn::Module type");
|
| 168 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 169 |
+
const auto module = modules_[index]->as<T>();
|
| 170 |
+
TORCH_CHECK(
|
| 171 |
+
module,
|
| 172 |
+
"Unable to cast module[",
|
| 173 |
+
index,
|
| 174 |
+
"] to ",
|
| 175 |
+
c10::demangle(typeid(T).name()));
|
| 176 |
+
return *module;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
/// Attempts to return a `std::shared_ptr` whose dynamic type is that of the
|
| 180 |
+
/// underlying module at the given index. Throws an exception if the index is
|
| 181 |
+
/// out of bounds.
|
| 182 |
+
std::shared_ptr<Module> ptr(size_t index) const {
|
| 183 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 184 |
+
return modules_[index];
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
/// Attempts to return a `std::shared_ptr` whose type is the one provided.
|
| 188 |
+
/// Throws an exception if the index is out of bounds or the types do not
|
| 189 |
+
/// match.
|
| 190 |
+
template <typename T>
|
| 191 |
+
std::shared_ptr<T> ptr(size_t index) const {
|
| 192 |
+
static_assert(
|
| 193 |
+
torch::detail::is_module<T>::value,
|
| 194 |
+
"Can only call ModuleList::ptr with an nn::Module type");
|
| 195 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 196 |
+
return std::dynamic_pointer_cast<T>(modules_[index]);
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
/// Like `ptr(index)`.
|
| 200 |
+
std::shared_ptr<Module> operator[](size_t index) const {
|
| 201 |
+
// This is the only method we can call without a type.
|
| 202 |
+
return ptr(index);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
/// The current size of the `ModuleList` container.
|
| 206 |
+
size_t size() const noexcept {
|
| 207 |
+
return modules_.size();
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
/// True if there are no modules in the `ModuleList`.
|
| 211 |
+
bool is_empty() const noexcept {
|
| 212 |
+
return size() == 0;
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
void insert(size_t index, std::shared_ptr<Module> module) {
|
| 216 |
+
TORCH_CHECK(index <= size(), "Index out of range");
|
| 217 |
+
|
| 218 |
+
if (index == size())
|
| 219 |
+
push_back(std::move(module));
|
| 220 |
+
else {
|
| 221 |
+
modules_.insert(
|
| 222 |
+
modules_.begin() + Iterator::difference_type(index),
|
| 223 |
+
std::move(module));
|
| 224 |
+
|
| 225 |
+
for (const auto i : c10::irange(index, size() - 1)) {
|
| 226 |
+
(void)i; // Suppress unused variable warning
|
| 227 |
+
replace_module(c10::to_string(index), modules_[index]);
|
| 228 |
+
}
|
| 229 |
+
register_module(c10::to_string(size() - 1), modules_.back());
|
| 230 |
+
}
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
/// Unwraps the contained module of a `ModuleHolder` and inserts it in the
|
| 234 |
+
/// `ModuleList`.
|
| 235 |
+
template <typename M>
|
| 236 |
+
void insert(size_t index, const ModuleHolder<M>& module_holder) {
|
| 237 |
+
insert(index, module_holder.ptr());
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
/// inserts a new `Module` to the `ModuleList` container, moving or copying
|
| 241 |
+
/// it into a `shared_ptr` internally. This method allows passing value types,
|
| 242 |
+
/// and letting the container deal with the boxing.
|
| 243 |
+
template <typename M, typename = torch::detail::enable_if_module_t<M>>
|
| 244 |
+
void insert(size_t index, M&& module) {
|
| 245 |
+
using Type = typename std::remove_reference<M>::type;
|
| 246 |
+
insert(index, std::make_shared<Type>(std::forward<M>(module)));
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
private:
|
| 250 |
+
template <typename Head, typename... Tail>
|
| 251 |
+
void push_back_var(Head&& head, Tail&&... tail) {
|
| 252 |
+
push_back(std::forward<Head>(head));
|
| 253 |
+
// Recursively calls this method, until the parameter pack only thas this
|
| 254 |
+
// entry left. Then calls `push_back()` a final time (above).
|
| 255 |
+
push_back_var(std::forward<Tail>(tail)...);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
/// The base case, when the list of modules is empty.
|
| 259 |
+
void push_back_var() {}
|
| 260 |
+
|
| 261 |
+
// Box the AnyModules to give ModuleList reference semantics, like the rest of
|
| 262 |
+
// the API. Note that this is not required otherwise, this could just be a
|
| 263 |
+
// `vector<AnyModule>`.
|
| 264 |
+
std::vector<std::shared_ptr<Module>> modules_;
|
| 265 |
+
};
|
| 266 |
+
|
| 267 |
+
/// A `ModuleHolder` subclass for `ModuleListImpl`.
|
| 268 |
+
/// See the documentation for `ModuleListImpl` class to learn what methods it
|
| 269 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 270 |
+
/// module storage semantics.
|
| 271 |
+
TORCH_MODULE(ModuleList);
|
| 272 |
+
|
| 273 |
+
} // namespace nn
|
| 274 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/named_any.h
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/detail/static.h>
|
| 4 |
+
#include <torch/nn/module.h>
|
| 5 |
+
#include <torch/nn/modules/container/any.h>
|
| 6 |
+
#include <torch/nn/pimpl.h>
|
| 7 |
+
#include <torch/types.h>
|
| 8 |
+
|
| 9 |
+
#include <torch/csrc/autograd/variable.h>
|
| 10 |
+
#include <torch/csrc/utils/variadic.h>
|
| 11 |
+
|
| 12 |
+
#include <ATen/Device.h>
|
| 13 |
+
|
| 14 |
+
#include <initializer_list>
|
| 15 |
+
#include <memory>
|
| 16 |
+
#include <type_traits>
|
| 17 |
+
#include <typeinfo>
|
| 18 |
+
#include <utility>
|
| 19 |
+
#include <vector>
|
| 20 |
+
|
| 21 |
+
namespace torch {
|
| 22 |
+
namespace nn {
|
| 23 |
+
|
| 24 |
+
/// Stores a type erased `Module` with name.
|
| 25 |
+
///
|
| 26 |
+
/// The `NamedAnyModule` class enables the following API for constructing
|
| 27 |
+
/// `nn::Sequential` with named submodules:
|
| 28 |
+
/// \rst
|
| 29 |
+
/// .. code-block:: cpp
|
| 30 |
+
///
|
| 31 |
+
/// struct M : torch::nn::Module {
|
| 32 |
+
/// explicit M(int value_) : value(value_) {}
|
| 33 |
+
/// int value;
|
| 34 |
+
/// int forward() {
|
| 35 |
+
/// return value;
|
| 36 |
+
/// }
|
| 37 |
+
/// };
|
| 38 |
+
///
|
| 39 |
+
/// Sequential sequential({
|
| 40 |
+
/// {"m1", std::make_shared<M>(1)}, // shared pointer to `Module` is
|
| 41 |
+
/// supported {std::string("m2"), M(2)}, // `Module` is supported
|
| 42 |
+
/// {"linear1", Linear(10, 3)} // `ModuleHolder` is supported
|
| 43 |
+
/// });
|
| 44 |
+
/// \endrst
|
| 45 |
+
class NamedAnyModule {
|
| 46 |
+
public:
|
| 47 |
+
/// Creates a `NamedAnyModule` from a (boxed) `Module`.
|
| 48 |
+
template <typename ModuleType>
|
| 49 |
+
NamedAnyModule(std::string name, std::shared_ptr<ModuleType> module_ptr)
|
| 50 |
+
: NamedAnyModule(std::move(name), AnyModule(std::move(module_ptr))) {}
|
| 51 |
+
|
| 52 |
+
/// Creates a `NamedAnyModule` from a `Module`, moving or copying it
|
| 53 |
+
/// into a `shared_ptr` internally.
|
| 54 |
+
// NOTE: We need to use `std::remove_reference<M>::type` to get rid of
|
| 55 |
+
// any reference components for make_unique.
|
| 56 |
+
template <typename M, typename = torch::detail::enable_if_module_t<M>>
|
| 57 |
+
NamedAnyModule(std::string name, M&& module)
|
| 58 |
+
: NamedAnyModule(
|
| 59 |
+
std::move(name),
|
| 60 |
+
std::make_shared<typename std::remove_reference<M>::type>(
|
| 61 |
+
std::forward<M>(module))) {}
|
| 62 |
+
|
| 63 |
+
/// Creates a `NamedAnyModule` from a `Module` that is unwrapped from
|
| 64 |
+
/// a `ModuleHolder`.
|
| 65 |
+
template <typename M>
|
| 66 |
+
NamedAnyModule(std::string name, const ModuleHolder<M>& module_holder)
|
| 67 |
+
: NamedAnyModule(std::move(name), module_holder.ptr()) {}
|
| 68 |
+
|
| 69 |
+
/// Creates a `NamedAnyModule` from a type-erased `AnyModule`.
|
| 70 |
+
NamedAnyModule(std::string name, AnyModule any_module)
|
| 71 |
+
: name_(std::move(name)), module_(std::move(any_module)) {}
|
| 72 |
+
|
| 73 |
+
/// Returns a reference to the name.
|
| 74 |
+
const std::string& name() const noexcept {
|
| 75 |
+
return name_;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/// Returns a reference to the module.
|
| 79 |
+
AnyModule& module() noexcept {
|
| 80 |
+
return module_;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/// Returns a const reference to the module.
|
| 84 |
+
const AnyModule& module() const noexcept {
|
| 85 |
+
return module_;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
private:
|
| 89 |
+
std::string name_;
|
| 90 |
+
AnyModule module_;
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
} // namespace nn
|
| 94 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/parameterdict.h
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/cloneable.h>
|
| 4 |
+
#include <torch/nn/pimpl.h>
|
| 5 |
+
#include <torch/ordered_dict.h>
|
| 6 |
+
#include <utility>
|
| 7 |
+
#include <vector>
|
| 8 |
+
|
| 9 |
+
namespace torch {
|
| 10 |
+
namespace nn {
|
| 11 |
+
|
| 12 |
+
class ParameterDictImpl : public Cloneable<ParameterDictImpl> {
|
| 13 |
+
public:
|
| 14 |
+
using Iterator = OrderedDict<std::string, Tensor>::Iterator;
|
| 15 |
+
using ConstIterator = OrderedDict<std::string, Tensor>::ConstIterator;
|
| 16 |
+
|
| 17 |
+
ParameterDictImpl() = default;
|
| 18 |
+
|
| 19 |
+
explicit ParameterDictImpl(
|
| 20 |
+
const torch::OrderedDict<std::string, torch::Tensor>& params) {
|
| 21 |
+
parameters_ = params;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
/// `reset()` is empty for `ParameterDict`, since it does not have
|
| 25 |
+
/// parameters of its own.
|
| 26 |
+
void reset() override {}
|
| 27 |
+
|
| 28 |
+
/// Pretty prints the `ParameterDict` module into the given `stream`.
|
| 29 |
+
void pretty_print(std::ostream& stream) const override {
|
| 30 |
+
stream << "torch::nn::ParameterDict(" << std::endl;
|
| 31 |
+
for (const auto& pair : parameters_) {
|
| 32 |
+
stream << "(" << pair.key() << ")"
|
| 33 |
+
<< ": Parameter containing: [" << pair.value().scalar_type()
|
| 34 |
+
<< " of size " << pair.value().sizes() << "]";
|
| 35 |
+
;
|
| 36 |
+
stream << std::endl;
|
| 37 |
+
}
|
| 38 |
+
stream << ")";
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
/// Insert the parameter along with the key into ParameterDict
|
| 42 |
+
/// The parameter is set to be require grad by default
|
| 43 |
+
Tensor& insert(std::string key, Tensor param) {
|
| 44 |
+
bool requires_grad = param.requires_grad();
|
| 45 |
+
return register_parameter(std::move(key), std::move(param), requires_grad);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
/// Remove key from the ParameterDict and return its value, throw exception
|
| 49 |
+
/// if the key is not contained. Please check contains(key) before for a
|
| 50 |
+
/// non-throwing access.
|
| 51 |
+
Tensor pop(const std::string& key) {
|
| 52 |
+
torch::Tensor v = parameters_[key];
|
| 53 |
+
parameters_.erase(key);
|
| 54 |
+
return v;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/// Return the keys in the dict
|
| 58 |
+
::std::vector<std::string> keys() const {
|
| 59 |
+
return parameters_.keys();
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
/// Return the Values in the dict
|
| 63 |
+
::std::vector<torch::Tensor> values() const {
|
| 64 |
+
return parameters_.values();
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
/// Return an iterator to the start of ParameterDict
|
| 68 |
+
Iterator begin() {
|
| 69 |
+
return parameters_.begin();
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/// Return a const iterator to the start of ParameterDict
|
| 73 |
+
ConstIterator begin() const {
|
| 74 |
+
return parameters_.begin();
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
/// Return an iterator to the end of ParameterDict
|
| 78 |
+
Iterator end() {
|
| 79 |
+
return parameters_.end();
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
/// Return a const iterator to the end of ParameterDict
|
| 83 |
+
ConstIterator end() const {
|
| 84 |
+
return parameters_.end();
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/// Return the number of items currently stored in the ParameterDict
|
| 88 |
+
size_t size() const noexcept {
|
| 89 |
+
return parameters_.size();
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
/// Return true if the ParameterDict is empty, otherwise return false
|
| 93 |
+
bool empty() const noexcept {
|
| 94 |
+
return parameters_.is_empty();
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
/// Update the ParameterDict with the key-value pairs from
|
| 98 |
+
/// another ParameterDict, overwriting existing key
|
| 99 |
+
template <typename Container>
|
| 100 |
+
void update(const Container& container) {
|
| 101 |
+
for (auto& item : container) {
|
| 102 |
+
parameters_[item.key()] = item.value();
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
/// Remove all parameters in the ParameterDict
|
| 107 |
+
void clear() {
|
| 108 |
+
parameters_.clear();
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
/// Check if the centain parameter with the key in the ParameterDict
|
| 112 |
+
bool contains(const std::string& key) const noexcept {
|
| 113 |
+
return parameters_.contains(key);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 117 |
+
/// no such key is stored in the `ParameterDict`. Check contains(key) before
|
| 118 |
+
/// for a non-throwing way of access
|
| 119 |
+
const Tensor& get(const std::string& key) const {
|
| 120 |
+
return parameters_[key];
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 124 |
+
/// no such key is stored in the `ParameterDict`. Check contains(key) before
|
| 125 |
+
/// for a non-throwing way of access
|
| 126 |
+
Tensor& get(const std::string& key) {
|
| 127 |
+
return parameters_[key];
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 131 |
+
/// no such key is stored in the `ParameterDict`. Check contains(key) before
|
| 132 |
+
/// for a non-throwing way of access
|
| 133 |
+
Tensor& operator[](const std::string& key) {
|
| 134 |
+
return parameters_[key];
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 138 |
+
/// no such key is stored in the `ParameterDict`. Check contains(key) before
|
| 139 |
+
/// for a non-throwing way of access
|
| 140 |
+
const Tensor& operator[](const std::string& key) const {
|
| 141 |
+
return parameters_[key];
|
| 142 |
+
}
|
| 143 |
+
};
|
| 144 |
+
|
| 145 |
+
TORCH_MODULE(ParameterDict);
|
| 146 |
+
|
| 147 |
+
} // namespace nn
|
| 148 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/parameterlist.h
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/cloneable.h>
|
| 4 |
+
#include <torch/nn/module.h>
|
| 5 |
+
|
| 6 |
+
#include <vector>
|
| 7 |
+
|
| 8 |
+
namespace torch {
|
| 9 |
+
namespace nn {
|
| 10 |
+
class ParameterListImpl : public Cloneable<ParameterListImpl> {
|
| 11 |
+
public:
|
| 12 |
+
using Iterator = typename std::vector<
|
| 13 |
+
OrderedDict<std::string, torch::Tensor>::Item>::iterator;
|
| 14 |
+
using ConstIterator = typename std::vector<
|
| 15 |
+
OrderedDict<std::string, torch::Tensor>::Item>::const_iterator;
|
| 16 |
+
|
| 17 |
+
ParameterListImpl() = default;
|
| 18 |
+
|
| 19 |
+
/// Constructs the `ParameterList` from a variadic list of ParameterList.
|
| 20 |
+
template <typename... Tensors>
|
| 21 |
+
explicit ParameterListImpl(Tensors&&... params) {
|
| 22 |
+
parameters_.reserve(sizeof...(Tensors));
|
| 23 |
+
push_back_var(std::forward<Tensors>(params)...);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
template <typename... Tensors>
|
| 27 |
+
explicit ParameterListImpl(const Tensors&... params) {
|
| 28 |
+
parameters_.reserve(sizeof...(Tensors));
|
| 29 |
+
push_back_var(std::forward<Tensors>(params)...);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
/// `reset()` is empty for `ParameterList`, since it does not have parameters
|
| 33 |
+
/// of its own.
|
| 34 |
+
void reset() override {}
|
| 35 |
+
|
| 36 |
+
/// Pretty prints the `ParameterList` module into the given `stream`.
|
| 37 |
+
void pretty_print(std::ostream& stream) const override {
|
| 38 |
+
stream << "torch::nn::ParameterList(" << std::endl;
|
| 39 |
+
for (const auto& pair : parameters_) {
|
| 40 |
+
stream << "(" << pair.key() << ")"
|
| 41 |
+
<< ": Parameter containing: [" << pair.value().scalar_type()
|
| 42 |
+
<< " of size " << pair.value().sizes() << "]";
|
| 43 |
+
;
|
| 44 |
+
stream << std::endl;
|
| 45 |
+
}
|
| 46 |
+
stream << ")";
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
/// push the a given parameter at the end of the list
|
| 50 |
+
void append(torch::Tensor&& param) {
|
| 51 |
+
bool requires_grad = param.requires_grad();
|
| 52 |
+
register_parameter(
|
| 53 |
+
c10::to_string(parameters_.size()), std::move(param), requires_grad);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
/// push the a given parameter at the end of the list
|
| 57 |
+
void append(const torch::Tensor& param) {
|
| 58 |
+
bool requires_grad = param.requires_grad();
|
| 59 |
+
register_parameter(
|
| 60 |
+
c10::to_string(parameters_.size()), param, requires_grad);
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/// push the a given parameter at the end of the list
|
| 64 |
+
/// And the key of the pair will be discarded, only the value
|
| 65 |
+
/// will be added into the `ParameterList`
|
| 66 |
+
void append(const OrderedDict<std::string, torch::Tensor>::Item& pair) {
|
| 67 |
+
register_parameter(
|
| 68 |
+
c10::to_string(parameters_.size()),
|
| 69 |
+
pair.value(),
|
| 70 |
+
pair.value().requires_grad());
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
/// extend parameters from a container to the end of the list
|
| 74 |
+
template <typename Container>
|
| 75 |
+
void extend(const Container& container) {
|
| 76 |
+
for (const auto& param : container) {
|
| 77 |
+
append(param);
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
/// Returns an iterator to the start of the ParameterList
|
| 82 |
+
/// the iterator returned will be type of `OrderedDict<std::string,
|
| 83 |
+
/// torch::Tensor>::Item`
|
| 84 |
+
Iterator begin() {
|
| 85 |
+
return parameters_.begin();
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
/// Returns a const iterator to the start of the ParameterList
|
| 89 |
+
/// the iterator returned will be type of `OrderedDict<std::string,
|
| 90 |
+
/// torch::Tensor>::Item`
|
| 91 |
+
ConstIterator begin() const {
|
| 92 |
+
return parameters_.begin();
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
/// Returns an iterator to the end of the ParameterList
|
| 96 |
+
/// the iterator returned will be type of `OrderedDict<std::string,
|
| 97 |
+
/// torch::Tensor>::Item`
|
| 98 |
+
Iterator end() {
|
| 99 |
+
return parameters_.end();
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/// Returns a const iterator to the end of the ParameterList
|
| 103 |
+
/// the iterator returned will be type of `OrderedDict<std::string,
|
| 104 |
+
/// torch::Tensor>::Item`
|
| 105 |
+
ConstIterator end() const {
|
| 106 |
+
return parameters_.end();
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 110 |
+
/// no such key is stored in the `ParameterList`. Check contains(key) before
|
| 111 |
+
/// for a non-throwing way of access
|
| 112 |
+
at::Tensor& at(size_t idx) {
|
| 113 |
+
TORCH_CHECK(idx < size(), "Index out of range");
|
| 114 |
+
return parameters_[c10::to_string(idx)];
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 118 |
+
/// no such key is stored in the `ParameterList`. Check contains(key) before
|
| 119 |
+
/// for a non-throwing way of access
|
| 120 |
+
const at::Tensor& at(size_t idx) const {
|
| 121 |
+
TORCH_CHECK(idx < size(), "Index out of range");
|
| 122 |
+
return parameters_[c10::to_string(idx)];
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 126 |
+
/// no such key is stored in the `ParameterList`. Check contains(key) before
|
| 127 |
+
/// for a non-throwing way of access
|
| 128 |
+
at::Tensor& operator[](size_t idx) {
|
| 129 |
+
return at(idx);
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
/// Returns the value associated with the given `key`. Throws an exception if
|
| 133 |
+
/// no such key is stored in the `ParameterList`. Check contains(key) before
|
| 134 |
+
/// for a non-throwing way of access
|
| 135 |
+
const at::Tensor& operator[](size_t idx) const {
|
| 136 |
+
return at(idx);
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
/// Return the size of the ParameterList
|
| 140 |
+
size_t size() const noexcept {
|
| 141 |
+
return parameters_.size();
|
| 142 |
+
}
|
| 143 |
+
/// True if the ParameterList is empty
|
| 144 |
+
bool is_empty() const noexcept {
|
| 145 |
+
return parameters_.is_empty();
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
/// Overload the +=, so that two ParameterList could be incrementally added
|
| 149 |
+
template <typename Container>
|
| 150 |
+
Container& operator+=(const Container& other) {
|
| 151 |
+
extend(other);
|
| 152 |
+
return *this;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
private:
|
| 156 |
+
template <typename Head, typename... Tail>
|
| 157 |
+
void push_back_var(Head&& head, Tail&&... tail) {
|
| 158 |
+
append(std::forward<Head>(head));
|
| 159 |
+
// Recursively calls this method, until the parameter pack only thas this
|
| 160 |
+
// entry left. Then calls `push_back()` a final time (above).
|
| 161 |
+
push_back_var(std::forward<Tail>(tail)...);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
/// The base case, when the list of modules is empty.
|
| 165 |
+
void push_back_var() {}
|
| 166 |
+
};
|
| 167 |
+
TORCH_MODULE(ParameterList);
|
| 168 |
+
} // namespace nn
|
| 169 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/container/sequential.h
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/detail/static.h>
|
| 4 |
+
#include <torch/nn/cloneable.h>
|
| 5 |
+
#include <torch/nn/module.h>
|
| 6 |
+
#include <torch/nn/modules/container/any.h>
|
| 7 |
+
#include <torch/nn/modules/container/named_any.h>
|
| 8 |
+
#include <torch/nn/pimpl.h>
|
| 9 |
+
#include <torch/types.h>
|
| 10 |
+
|
| 11 |
+
#include <c10/util/Exception.h>
|
| 12 |
+
|
| 13 |
+
#include <cstdint>
|
| 14 |
+
#include <memory>
|
| 15 |
+
#include <ostream>
|
| 16 |
+
#include <string>
|
| 17 |
+
#include <type_traits>
|
| 18 |
+
#include <utility>
|
| 19 |
+
#include <vector>
|
| 20 |
+
|
| 21 |
+
namespace torch {
|
| 22 |
+
namespace nn {
|
| 23 |
+
|
| 24 |
+
/// A list of `Module`s that acts as a `Module` itself.
|
| 25 |
+
///
|
| 26 |
+
/// A `Sequential` is fundamentally a list of `Module`s, each with a `forward()`
|
| 27 |
+
/// method. `Sequential` provides a `forward()` method of its own, which accepts
|
| 28 |
+
/// any input and forwards it to the first module it stores. It then "chains"
|
| 29 |
+
/// outputs to inputs sequentially for each subsequent module, finally returning
|
| 30 |
+
/// the output of the last module. For example:
|
| 31 |
+
///
|
| 32 |
+
/// \rst
|
| 33 |
+
/// .. code-block:: cpp
|
| 34 |
+
///
|
| 35 |
+
/// torch::nn::Sequential seq(
|
| 36 |
+
/// torch::nn::Linear(3, 4),
|
| 37 |
+
/// torch::nn::BatchNorm1d(4),
|
| 38 |
+
/// torch::nn::Dropout(0.5)
|
| 39 |
+
/// );
|
| 40 |
+
///
|
| 41 |
+
/// auto output = seq->forward(torch::ones(3));
|
| 42 |
+
///
|
| 43 |
+
/// \endrst
|
| 44 |
+
///
|
| 45 |
+
/// This can conceptually be thought of as the following loop (using Python as
|
| 46 |
+
/// pseudocode):
|
| 47 |
+
///
|
| 48 |
+
/// \rst
|
| 49 |
+
/// .. code-block:: python
|
| 50 |
+
///
|
| 51 |
+
/// def forward(sequential, input):
|
| 52 |
+
/// for module in sequential:
|
| 53 |
+
/// input = module(input)
|
| 54 |
+
/// return input
|
| 55 |
+
///
|
| 56 |
+
/// \endrst
|
| 57 |
+
///
|
| 58 |
+
/// Why should you use `Sequential` instead of a simple `std::vector`? The value
|
| 59 |
+
/// a `Sequential` provides over manually calling a sequence of modules is that
|
| 60 |
+
/// it allows treating the whole container *as a single module*, such that
|
| 61 |
+
/// performing a transformation on the `Sequential` applies to each of the
|
| 62 |
+
/// modules it stores (which are each a registered submodule of the
|
| 63 |
+
/// `Sequential`). For example, calling
|
| 64 |
+
/// `.to(torch::kCUDA)` on a `Sequential` will move each module in the list to
|
| 65 |
+
/// CUDA memory. For example:
|
| 66 |
+
///
|
| 67 |
+
/// \rst
|
| 68 |
+
/// .. code-block:: cpp
|
| 69 |
+
///
|
| 70 |
+
/// torch::nn::Sequential seq(
|
| 71 |
+
/// torch::nn::Linear(3, 4),
|
| 72 |
+
/// torch::nn::BatchNorm1d(4),
|
| 73 |
+
/// torch::nn::Dropout(0.5)
|
| 74 |
+
/// );
|
| 75 |
+
///
|
| 76 |
+
/// // Convert all modules to CUDA.
|
| 77 |
+
/// seq->to(torch::kCUDA);
|
| 78 |
+
///
|
| 79 |
+
/// \endrst
|
| 80 |
+
///
|
| 81 |
+
/// Finally, `Sequential` provides a lightweight container API, such as allowing
|
| 82 |
+
/// iteration over submodules, positional access, adding a new module after
|
| 83 |
+
/// construction via `push_back`, as well as joining two `Sequential`s via
|
| 84 |
+
/// `extend`.
|
| 85 |
+
///
|
| 86 |
+
/// \rst
|
| 87 |
+
/// .. attention::
|
| 88 |
+
/// One current limitation of `Sequential` is that all except the first module
|
| 89 |
+
/// must accept a single argument. If your modules need to take multiple
|
| 90 |
+
/// arguments, you should define them to take and return tuples.
|
| 91 |
+
/// \endrst
|
| 92 |
+
class SequentialImpl : public Cloneable<SequentialImpl> {
|
| 93 |
+
public:
|
| 94 |
+
using Iterator = std::vector<AnyModule>::iterator;
|
| 95 |
+
using ConstIterator = std::vector<AnyModule>::const_iterator;
|
| 96 |
+
|
| 97 |
+
SequentialImpl() = default;
|
| 98 |
+
|
| 99 |
+
/// Constructs the `Sequential` from a variadic list of modules.
|
| 100 |
+
template <typename... Modules>
|
| 101 |
+
explicit SequentialImpl(Modules&&... modules) {
|
| 102 |
+
modules_.reserve(sizeof...(Modules));
|
| 103 |
+
push_back(std::forward<Modules>(modules)...);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
/// Constructs the `Sequential` from an `OrderedDict` of named `AnyModule`s.
|
| 107 |
+
explicit SequentialImpl(
|
| 108 |
+
torch::OrderedDict<std::string, AnyModule>&& ordered_dict) {
|
| 109 |
+
modules_.reserve(ordered_dict.size());
|
| 110 |
+
for (auto& item : ordered_dict) {
|
| 111 |
+
push_back(item.key(), std::move(item.value()));
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
/// Constructs the `Sequential` from a braced-init-list of named `AnyModule`s.
|
| 116 |
+
/// It enables the following use case:
|
| 117 |
+
/// `Sequential sequential({{"m1", M(1)}, {"m2", M(2)}})`
|
| 118 |
+
explicit SequentialImpl(std::initializer_list<NamedAnyModule> named_modules) {
|
| 119 |
+
modules_.reserve(named_modules.size());
|
| 120 |
+
for (const auto& named_module : named_modules) {
|
| 121 |
+
push_back(named_module.name(), named_module.module());
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
/// Special cloning function for `Sequential` because it does not use
|
| 126 |
+
/// `reset()`.
|
| 127 |
+
std::shared_ptr<Module> clone(
|
| 128 |
+
const optional<Device>& device = nullopt) const override {
|
| 129 |
+
auto clone = std::make_shared<SequentialImpl>();
|
| 130 |
+
for (const auto& module : modules_) {
|
| 131 |
+
clone->push_back(module.clone(device));
|
| 132 |
+
}
|
| 133 |
+
return clone;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
/// `reset()` is empty for `Sequential`, since it does not have parameters of
|
| 137 |
+
/// its own.
|
| 138 |
+
void reset() override {}
|
| 139 |
+
|
| 140 |
+
/// Pretty prints the `Sequential` module into the given `stream`.
|
| 141 |
+
void pretty_print(std::ostream& stream) const override {
|
| 142 |
+
stream << "torch::nn::Sequential";
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
/// Feeds `inputs` to the first module and then chains outputs to inputs,
|
| 146 |
+
/// returning the last output.
|
| 147 |
+
///
|
| 148 |
+
/// Conceptually the following loop in Python:
|
| 149 |
+
///
|
| 150 |
+
/// \rst
|
| 151 |
+
/// .. code-block:: python
|
| 152 |
+
///
|
| 153 |
+
/// def forward(sequential, input):
|
| 154 |
+
/// for module in sequential:
|
| 155 |
+
/// input = module(input)
|
| 156 |
+
/// return input
|
| 157 |
+
///
|
| 158 |
+
/// \endrst
|
| 159 |
+
///
|
| 160 |
+
/// The return type is taken as the first template parameter. It defaults to
|
| 161 |
+
/// `Tensor`. If the last module in the `Sequential` returns another type `T`,
|
| 162 |
+
/// you should call `forward<T>(inputs)` instead of just `forward(inputs)`:
|
| 163 |
+
///
|
| 164 |
+
/// \rst
|
| 165 |
+
/// .. code-block:: cpp
|
| 166 |
+
///
|
| 167 |
+
/// torch::Tensor tensor = sequential1->forward(inputs);
|
| 168 |
+
/// int integer = sequential2->forward<int>(inputs);
|
| 169 |
+
/// float value = sequential3->forward<float>(inputs);
|
| 170 |
+
///
|
| 171 |
+
/// \endrst
|
| 172 |
+
template <typename ReturnType = Tensor, typename... InputTypes>
|
| 173 |
+
ReturnType forward(InputTypes&&... inputs) {
|
| 174 |
+
TORCH_CHECK(!is_empty(), "Cannot call forward() on an empty Sequential");
|
| 175 |
+
|
| 176 |
+
auto iterator = modules_.begin();
|
| 177 |
+
auto input = iterator->any_forward(std::forward<InputTypes>(inputs)...);
|
| 178 |
+
|
| 179 |
+
for (++iterator; iterator != modules_.end(); ++iterator) {
|
| 180 |
+
input = iterator->any_forward(std::move(input));
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// Check the return value and give a nice error message if the requested
|
| 184 |
+
// return type was incorrect.
|
| 185 |
+
if (auto* return_value = input.template try_get<ReturnType>()) {
|
| 186 |
+
return std::move(*return_value);
|
| 187 |
+
}
|
| 188 |
+
AT_ERROR(
|
| 189 |
+
"The type of the return value is ",
|
| 190 |
+
c10::demangle(input.type_info().name()),
|
| 191 |
+
", but you asked for type ",
|
| 192 |
+
c10::demangle(typeid(ReturnType).name()));
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
/// Adds a new (boxed) `Module` to the `Sequential` container.
|
| 196 |
+
template <typename ModuleType>
|
| 197 |
+
void push_back(std::shared_ptr<ModuleType> module_ptr) {
|
| 198 |
+
push_back(c10::to_string(modules_.size()), std::move(module_ptr));
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
/// Adds a new named (boxed) `Module` to the `Sequential` container.
|
| 202 |
+
template <typename ModuleType>
|
| 203 |
+
void push_back(std::string name, std::shared_ptr<ModuleType> module_ptr) {
|
| 204 |
+
push_back(std::move(name), AnyModule(std::move(module_ptr)));
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
/// Adds a new `Module` to the `Sequential` container, moving or copying it
|
| 208 |
+
/// into a `shared_ptr` internally. This method allows passing value types,
|
| 209 |
+
/// and letting the container deal with the boxing. This means you can write
|
| 210 |
+
/// `Sequential(Module(3, 4))` instead of
|
| 211 |
+
/// `Sequential(std::make_shared<Module>(3, 4))`.
|
| 212 |
+
template <typename M, typename = torch::detail::enable_if_module_t<M>>
|
| 213 |
+
void push_back(M&& module) {
|
| 214 |
+
push_back(c10::to_string(modules_.size()), std::forward<M>(module));
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
/// Adds a new named `Module` to the `Sequential` container, moving or copying
|
| 218 |
+
/// it into a `shared_ptr` internally. This method allows passing value types,
|
| 219 |
+
/// and letting the container deal with the boxing.
|
| 220 |
+
template <typename M, typename = torch::detail::enable_if_module_t<M>>
|
| 221 |
+
void push_back(std::string name, M&& module) {
|
| 222 |
+
using Type = typename std::remove_reference<M>::type;
|
| 223 |
+
push_back(std::move(name), std::make_shared<Type>(std::forward<M>(module)));
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
/// Unwraps the contained module of a `ModuleHolder` and adds it to the
|
| 227 |
+
/// `Sequential`.
|
| 228 |
+
template <typename M>
|
| 229 |
+
void push_back(const ModuleHolder<M>& module_holder) {
|
| 230 |
+
push_back(c10::to_string(modules_.size()), module_holder);
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
/// Unwraps the contained named module of a `ModuleHolder` and adds it to the
|
| 234 |
+
/// `Sequential`.
|
| 235 |
+
template <typename M>
|
| 236 |
+
void push_back(std::string name, const ModuleHolder<M>& module_holder) {
|
| 237 |
+
push_back(std::move(name), module_holder.ptr());
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
/// Iterates over the container and calls `push_back()` on each value.
|
| 241 |
+
template <typename Container>
|
| 242 |
+
void extend(const Container& container) {
|
| 243 |
+
for (const auto& module : container) {
|
| 244 |
+
push_back(module);
|
| 245 |
+
}
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
/// Adds a type-erased `AnyModule` to the `Sequential`.
|
| 249 |
+
void push_back(AnyModule any_module) {
|
| 250 |
+
push_back(c10::to_string(modules_.size()), std::move(any_module));
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
void push_back(std::string name, AnyModule any_module) {
|
| 254 |
+
modules_.push_back(std::move(any_module));
|
| 255 |
+
const auto index = modules_.size() - 1;
|
| 256 |
+
register_module(std::move(name), modules_[index].ptr());
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
/// Returns an iterator to the start of the `Sequential`.
|
| 260 |
+
Iterator begin() {
|
| 261 |
+
return modules_.begin();
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
/// Returns a const iterator to the start of the `Sequential`.
|
| 265 |
+
ConstIterator begin() const {
|
| 266 |
+
return modules_.begin();
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
/// Returns an iterator to the end of the `Sequential`.
|
| 270 |
+
Iterator end() {
|
| 271 |
+
return modules_.end();
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
/// Returns a const iterator to the end of the `Sequential`.
|
| 275 |
+
ConstIterator end() const {
|
| 276 |
+
return modules_.end();
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
/// Attempts to return the module at the given index as the requested type.
|
| 280 |
+
/// Throws an exception if the index is out of bounds or the types do not
|
| 281 |
+
/// match.
|
| 282 |
+
template <typename T>
|
| 283 |
+
T& at(size_t index) {
|
| 284 |
+
static_assert(
|
| 285 |
+
torch::detail::is_module<T>::value,
|
| 286 |
+
"Can only call Sequential::at with an nn::Module type");
|
| 287 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 288 |
+
return modules_[index].get<T>();
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
/// Attempts to return the module at the given index as the requested type.
|
| 292 |
+
/// Throws an exception if the index is out of bounds or the types do not
|
| 293 |
+
/// match.
|
| 294 |
+
template <typename T>
|
| 295 |
+
const T& at(size_t index) const {
|
| 296 |
+
static_assert(
|
| 297 |
+
torch::detail::is_module<T>::value,
|
| 298 |
+
"Can only call Sequential::at with an nn::Module type");
|
| 299 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 300 |
+
return modules_[index].get<T>();
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
/// Attempts to return a `std::shared_ptr` whose dynamic type is that of the
|
| 304 |
+
/// underlying module at the given index. Throws an exception if the index is
|
| 305 |
+
/// out of bounds.
|
| 306 |
+
std::shared_ptr<Module> ptr(size_t index) const {
|
| 307 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 308 |
+
return modules_[index].ptr();
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
/// Attempts to return a `std::shared_ptr` whose type is the one provided.
|
| 312 |
+
/// Throws an exception if the index is out of bounds or the types do not
|
| 313 |
+
/// match.
|
| 314 |
+
template <typename T>
|
| 315 |
+
std::shared_ptr<T> ptr(size_t index) const {
|
| 316 |
+
static_assert(
|
| 317 |
+
torch::detail::is_module<T>::value,
|
| 318 |
+
"Can only call Sequential::ptr with an nn::Module type");
|
| 319 |
+
TORCH_CHECK(index < size(), "Index out of range");
|
| 320 |
+
return modules_[index].ptr<T>();
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
/// Like `ptr(index)`.
|
| 324 |
+
std::shared_ptr<Module> operator[](size_t index) const {
|
| 325 |
+
// This is the only method we can call without a type.
|
| 326 |
+
return ptr(index);
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
/// The current size of the `Sequential` container.
|
| 330 |
+
size_t size() const noexcept {
|
| 331 |
+
return modules_.size();
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
/// True if there are no modules in the `Sequential`.
|
| 335 |
+
bool is_empty() const noexcept {
|
| 336 |
+
return size() == 0;
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
private:
|
| 340 |
+
/// Takes a First *and* Second parameter, to avoid ambiguity when a parameter
|
| 341 |
+
/// pack has only one type, in which case the template would be preferred,
|
| 342 |
+
/// even if the other `push_back` functions are better fits (e.g. `unique_ptr`
|
| 343 |
+
/// -> `shared_ptr` overload).
|
| 344 |
+
/// NOTE: We explicitly avoid matching this template with
|
| 345 |
+
/// `push_back(std::string("name"), module)` or `push_back("name", module)`,
|
| 346 |
+
/// since they should be handled by their respective `push_back` functions.
|
| 347 |
+
template <
|
| 348 |
+
typename First,
|
| 349 |
+
typename Second,
|
| 350 |
+
typename... Rest,
|
| 351 |
+
typename = torch::disable_if_t<
|
| 352 |
+
std::is_same<First, std::string>::value ||
|
| 353 |
+
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
|
| 354 |
+
std::is_same<
|
| 355 |
+
typename std::decay<First>::type,
|
| 356 |
+
std::decay<const char (&)[]>::type>::value>>
|
| 357 |
+
void push_back(First&& first, Second&& second, Rest&&... rest) {
|
| 358 |
+
push_back(std::forward<First>(first));
|
| 359 |
+
// Recursively calls this method, until the parameter pack only thas this
|
| 360 |
+
// entry left. Then calls `push_back()` a final time (above).
|
| 361 |
+
push_back(std::forward<Second>(second), std::forward<Rest>(rest)...);
|
| 362 |
+
}
|
| 363 |
+
|
| 364 |
+
/// The base case, when the list of modules is empty.
|
| 365 |
+
void push_back() {}
|
| 366 |
+
|
| 367 |
+
// Box the AnyModules to give Sequential reference semantics, like the rest of
|
| 368 |
+
// the API. Note that this is not required otherwise, this could just be a
|
| 369 |
+
// `vector<AnyModule>`.
|
| 370 |
+
std::vector<AnyModule> modules_;
|
| 371 |
+
};
|
| 372 |
+
|
| 373 |
+
/// A `ModuleHolder` subclass for `SequentialImpl`.
|
| 374 |
+
/// See the documentation for `SequentialImpl` class to learn what methods it
|
| 375 |
+
/// provides, or the documentation for `ModuleHolder` to learn about PyTorch's
|
| 376 |
+
/// module storage semantics.
|
| 377 |
+
class Sequential : public torch::nn::ModuleHolder<SequentialImpl> {
|
| 378 |
+
public:
|
| 379 |
+
using torch::nn::ModuleHolder<SequentialImpl>::ModuleHolder;
|
| 380 |
+
|
| 381 |
+
Sequential() : ModuleHolder() {}
|
| 382 |
+
|
| 383 |
+
/// Constructs the `Sequential` from a braced-init-list of named `AnyModule`s.
|
| 384 |
+
/// It enables the following use case:
|
| 385 |
+
/// `Sequential sequential({{"m1", M(1)}, {"m2", M(2)}})`
|
| 386 |
+
Sequential(std::initializer_list<NamedAnyModule> named_modules)
|
| 387 |
+
: ModuleHolder(std::make_shared<SequentialImpl>(named_modules)) {}
|
| 388 |
+
};
|
| 389 |
+
} // namespace nn
|
| 390 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/distance.h
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/nn/cloneable.h>
|
| 4 |
+
#include <torch/nn/functional/distance.h>
|
| 5 |
+
#include <torch/nn/options/distance.h>
|
| 6 |
+
#include <torch/nn/pimpl.h>
|
| 7 |
+
#include <torch/types.h>
|
| 8 |
+
|
| 9 |
+
#include <torch/csrc/Export.h>
|
| 10 |
+
|
| 11 |
+
namespace torch {
|
| 12 |
+
namespace nn {
|
| 13 |
+
|
| 14 |
+
/// Returns the cosine similarity between :math:`x_1` and :math:`x_2`, computed
|
| 15 |
+
/// along `dim`.
|
| 16 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.CosineSimilarity to
|
| 17 |
+
/// learn about the exact behavior of this module.
|
| 18 |
+
///
|
| 19 |
+
/// See the documentation for `torch::nn::CosineSimilarityOptions` class to
|
| 20 |
+
/// learn what constructor arguments are supported for this module.
|
| 21 |
+
///
|
| 22 |
+
/// Example:
|
| 23 |
+
/// ```
|
| 24 |
+
/// CosineSimilarity model(CosineSimilarityOptions().dim(0).eps(0.5));
|
| 25 |
+
/// ```
|
| 26 |
+
class TORCH_API CosineSimilarityImpl : public Cloneable<CosineSimilarityImpl> {
|
| 27 |
+
public:
|
| 28 |
+
explicit CosineSimilarityImpl(const CosineSimilarityOptions& options_ = {});
|
| 29 |
+
|
| 30 |
+
void reset() override;
|
| 31 |
+
|
| 32 |
+
/// Pretty prints the `CosineSimilarity` module into the given `stream`.
|
| 33 |
+
void pretty_print(std::ostream& stream) const override;
|
| 34 |
+
|
| 35 |
+
Tensor forward(const Tensor& input1, const Tensor& input2);
|
| 36 |
+
|
| 37 |
+
/// The options with which this `Module` was constructed.
|
| 38 |
+
CosineSimilarityOptions options;
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
/// A `ModuleHolder` subclass for `CosineSimilarityImpl`.
|
| 42 |
+
/// See the documentation for `CosineSimilarityImpl` class to learn what methods
|
| 43 |
+
/// it provides, and examples of how to use `CosineSimilarity` with
|
| 44 |
+
/// `torch::nn::CosineSimilarityOptions`. See the documentation for
|
| 45 |
+
/// `ModuleHolder` to learn about PyTorch's module storage semantics.
|
| 46 |
+
TORCH_MODULE(CosineSimilarity);
|
| 47 |
+
|
| 48 |
+
// ============================================================================
|
| 49 |
+
|
| 50 |
+
/// Returns the batchwise pairwise distance between vectors :math:`v_1`,
|
| 51 |
+
/// :math:`v_2` using the p-norm.
|
| 52 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.PairwiseDistance to
|
| 53 |
+
/// learn about the exact behavior of this module.
|
| 54 |
+
///
|
| 55 |
+
/// See the documentation for `torch::nn::PairwiseDistanceOptions` class to
|
| 56 |
+
/// learn what constructor arguments are supported for this module.
|
| 57 |
+
///
|
| 58 |
+
/// Example:
|
| 59 |
+
/// ```
|
| 60 |
+
/// PairwiseDistance
|
| 61 |
+
/// model(PairwiseDistanceOptions().p(3).eps(0.5).keepdim(true));
|
| 62 |
+
/// ```
|
| 63 |
+
class TORCH_API PairwiseDistanceImpl : public Cloneable<PairwiseDistanceImpl> {
|
| 64 |
+
public:
|
| 65 |
+
explicit PairwiseDistanceImpl(const PairwiseDistanceOptions& options_ = {});
|
| 66 |
+
|
| 67 |
+
void reset() override;
|
| 68 |
+
|
| 69 |
+
/// Pretty prints the `PairwiseDistance` module into the given `stream`.
|
| 70 |
+
void pretty_print(std::ostream& stream) const override;
|
| 71 |
+
|
| 72 |
+
Tensor forward(const Tensor& input1, const Tensor& input2);
|
| 73 |
+
|
| 74 |
+
/// The options with which this `Module` was constructed.
|
| 75 |
+
PairwiseDistanceOptions options;
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
/// A `ModuleHolder` subclass for `PairwiseDistanceImpl`.
|
| 79 |
+
/// See the documentation for `PairwiseDistanceImpl` class to learn what methods
|
| 80 |
+
/// it provides, and examples of how to use `PairwiseDistance` with
|
| 81 |
+
/// `torch::nn::PairwiseDistanceOptions`. See the documentation for
|
| 82 |
+
/// `ModuleHolder` to learn about PyTorch's module storage semantics.
|
| 83 |
+
TORCH_MODULE(PairwiseDistance);
|
| 84 |
+
|
| 85 |
+
} // namespace nn
|
| 86 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/embedding.h
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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/options/embedding.h>
|
| 7 |
+
#include <torch/nn/pimpl.h>
|
| 8 |
+
#include <torch/types.h>
|
| 9 |
+
|
| 10 |
+
#include <cstddef>
|
| 11 |
+
|
| 12 |
+
namespace torch {
|
| 13 |
+
namespace nn {
|
| 14 |
+
|
| 15 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Embedding
|
| 16 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 17 |
+
|
| 18 |
+
/// Performs a lookup in a fixed size embedding table.
|
| 19 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Embedding to learn
|
| 20 |
+
/// about the exact behavior of this module.
|
| 21 |
+
///
|
| 22 |
+
/// See the documentation for `torch::nn::EmbeddingOptions` class to learn what
|
| 23 |
+
/// constructor arguments are supported for this module.
|
| 24 |
+
///
|
| 25 |
+
/// Example:
|
| 26 |
+
/// ```
|
| 27 |
+
/// Embedding model(EmbeddingOptions(10,
|
| 28 |
+
/// 2).padding_idx(3).max_norm(2).norm_type(2.5).scale_grad_by_freq(true).sparse(true));
|
| 29 |
+
/// ```
|
| 30 |
+
class TORCH_API EmbeddingImpl : public torch::nn::Cloneable<EmbeddingImpl> {
|
| 31 |
+
public:
|
| 32 |
+
EmbeddingImpl(int64_t num_embeddings, int64_t embedding_dim)
|
| 33 |
+
: EmbeddingImpl(EmbeddingOptions(num_embeddings, embedding_dim)) {}
|
| 34 |
+
explicit EmbeddingImpl(EmbeddingOptions options_);
|
| 35 |
+
|
| 36 |
+
void reset() override;
|
| 37 |
+
|
| 38 |
+
void reset_parameters();
|
| 39 |
+
|
| 40 |
+
/// Pretty prints the `Embedding` module into the given `stream`.
|
| 41 |
+
void pretty_print(std::ostream& stream) const override;
|
| 42 |
+
|
| 43 |
+
/// Performs a lookup on the embedding table stored in `weight` using the
|
| 44 |
+
/// `indices` supplied and returns the result.
|
| 45 |
+
Tensor forward(const Tensor& indices);
|
| 46 |
+
|
| 47 |
+
/// The `Options` used to configure this `Embedding` module.
|
| 48 |
+
/// Changes to `EmbeddingOptions` *after construction* have no effect.
|
| 49 |
+
EmbeddingOptions options;
|
| 50 |
+
|
| 51 |
+
/// The embedding table.
|
| 52 |
+
Tensor weight;
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
+
/// A `ModuleHolder` subclass for `EmbeddingImpl`.
|
| 56 |
+
/// See the documentation for `EmbeddingImpl` class to learn what methods it
|
| 57 |
+
/// provides, and examples of how to use `Embedding` with
|
| 58 |
+
/// `torch::nn::EmbeddingOptions`. See the documentation for `ModuleHolder` to
|
| 59 |
+
/// learn about PyTorch's module storage semantics.
|
| 60 |
+
class Embedding : public torch::nn::ModuleHolder<EmbeddingImpl> {
|
| 61 |
+
public:
|
| 62 |
+
using torch::nn::ModuleHolder<EmbeddingImpl>::ModuleHolder;
|
| 63 |
+
|
| 64 |
+
/// See the documentation for `torch::nn::EmbeddingFromPretrainedOptions`
|
| 65 |
+
/// class to learn what optional arguments are supported for this function.
|
| 66 |
+
static Embedding from_pretrained(
|
| 67 |
+
const torch::Tensor& embeddings,
|
| 68 |
+
const EmbeddingFromPretrainedOptions& options = {}) {
|
| 69 |
+
TORCH_CHECK(
|
| 70 |
+
embeddings.dim() == 2,
|
| 71 |
+
"Embeddings parameter is expected to be 2-dimensional");
|
| 72 |
+
|
| 73 |
+
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
| 74 |
+
int64_t rows, cols;
|
| 75 |
+
rows = embeddings.size(0);
|
| 76 |
+
cols = embeddings.size(1);
|
| 77 |
+
|
| 78 |
+
Embedding embedding(EmbeddingOptions(rows, cols)
|
| 79 |
+
._weight(embeddings)
|
| 80 |
+
.padding_idx(options.padding_idx())
|
| 81 |
+
.max_norm(options.max_norm())
|
| 82 |
+
.norm_type(options.norm_type())
|
| 83 |
+
.scale_grad_by_freq(options.scale_grad_by_freq())
|
| 84 |
+
.sparse(options.sparse()));
|
| 85 |
+
embedding->weight.set_requires_grad(!options.freeze());
|
| 86 |
+
return embedding;
|
| 87 |
+
}
|
| 88 |
+
};
|
| 89 |
+
|
| 90 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EmbeddingBag
|
| 91 |
+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 92 |
+
|
| 93 |
+
/// Computes sums or means of 'bags' of embeddings, without instantiating the
|
| 94 |
+
/// intermediate embeddings.
|
| 95 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.EmbeddingBag to learn
|
| 96 |
+
/// about the exact behavior of this module.
|
| 97 |
+
///
|
| 98 |
+
/// See the documentation for `torch::nn::EmbeddingBagOptions` class to learn
|
| 99 |
+
/// what constructor arguments are supported for this module.
|
| 100 |
+
///
|
| 101 |
+
/// Example:
|
| 102 |
+
/// ```
|
| 103 |
+
/// EmbeddingBag model(EmbeddingBagOptions(10,
|
| 104 |
+
/// 2).max_norm(2).norm_type(2.5).scale_grad_by_freq(true).sparse(true).mode(torch::kSum).padding_idx(1));
|
| 105 |
+
/// ```
|
| 106 |
+
class TORCH_API EmbeddingBagImpl
|
| 107 |
+
: public torch::nn::Cloneable<EmbeddingBagImpl> {
|
| 108 |
+
public:
|
| 109 |
+
EmbeddingBagImpl(int64_t num_embeddings, int64_t embedding_dim)
|
| 110 |
+
: EmbeddingBagImpl(EmbeddingBagOptions(num_embeddings, embedding_dim)) {}
|
| 111 |
+
explicit EmbeddingBagImpl(EmbeddingBagOptions options_);
|
| 112 |
+
|
| 113 |
+
void reset() override;
|
| 114 |
+
|
| 115 |
+
void reset_parameters();
|
| 116 |
+
|
| 117 |
+
/// Pretty prints the `EmbeddingBag` module into the given `stream`.
|
| 118 |
+
void pretty_print(std::ostream& stream) const override;
|
| 119 |
+
|
| 120 |
+
/// The `Options` used to configure this `EmbeddingBag` module.
|
| 121 |
+
EmbeddingBagOptions options;
|
| 122 |
+
/// The embedding table.
|
| 123 |
+
Tensor weight;
|
| 124 |
+
|
| 125 |
+
Tensor forward(
|
| 126 |
+
const Tensor& input,
|
| 127 |
+
const Tensor& offsets = {},
|
| 128 |
+
const Tensor& per_sample_weights = {});
|
| 129 |
+
|
| 130 |
+
protected:
|
| 131 |
+
FORWARD_HAS_DEFAULT_ARGS({1, AnyValue(Tensor())}, {2, AnyValue(Tensor())})
|
| 132 |
+
};
|
| 133 |
+
|
| 134 |
+
/// A `ModuleHolder` subclass for `EmbeddingBagImpl`.
|
| 135 |
+
/// See the documentation for `EmbeddingBagImpl` class to learn what methods it
|
| 136 |
+
/// provides, and examples of how to use `EmbeddingBag` with
|
| 137 |
+
/// `torch::nn::EmbeddingBagOptions`. See the documentation for `ModuleHolder`
|
| 138 |
+
/// to learn about PyTorch's module storage semantics.
|
| 139 |
+
class EmbeddingBag : public torch::nn::ModuleHolder<EmbeddingBagImpl> {
|
| 140 |
+
public:
|
| 141 |
+
using torch::nn::ModuleHolder<EmbeddingBagImpl>::ModuleHolder;
|
| 142 |
+
|
| 143 |
+
/// See the documentation for `torch::nn::EmbeddingBagFromPretrainedOptions`
|
| 144 |
+
/// class to learn what optional arguments are supported for this function.
|
| 145 |
+
static EmbeddingBag from_pretrained(
|
| 146 |
+
const torch::Tensor& embeddings,
|
| 147 |
+
const EmbeddingBagFromPretrainedOptions& options = {}) {
|
| 148 |
+
TORCH_CHECK(
|
| 149 |
+
embeddings.dim() == 2,
|
| 150 |
+
"Embeddings parameter is expected to be 2-dimensional");
|
| 151 |
+
|
| 152 |
+
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
|
| 153 |
+
int64_t rows, cols;
|
| 154 |
+
rows = embeddings.size(0);
|
| 155 |
+
cols = embeddings.size(1);
|
| 156 |
+
|
| 157 |
+
EmbeddingBag embeddingbag(
|
| 158 |
+
EmbeddingBagOptions(rows, cols)
|
| 159 |
+
._weight(embeddings)
|
| 160 |
+
.max_norm(options.max_norm())
|
| 161 |
+
.norm_type(options.norm_type())
|
| 162 |
+
.scale_grad_by_freq(options.scale_grad_by_freq())
|
| 163 |
+
.mode(options.mode())
|
| 164 |
+
.sparse(options.sparse())
|
| 165 |
+
.padding_idx(options.padding_idx()));
|
| 166 |
+
embeddingbag->weight.set_requires_grad(!options.freeze());
|
| 167 |
+
return embeddingbag;
|
| 168 |
+
}
|
| 169 |
+
};
|
| 170 |
+
} // namespace nn
|
| 171 |
+
} // namespace torch
|
videollama2/lib/python3.10/site-packages/torch/include/torch/csrc/api/include/torch/nn/modules/fold.h
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include <torch/expanding_array.h>
|
| 4 |
+
#include <torch/nn/cloneable.h>
|
| 5 |
+
#include <torch/nn/functional/fold.h>
|
| 6 |
+
#include <torch/nn/options/fold.h>
|
| 7 |
+
#include <torch/nn/pimpl.h>
|
| 8 |
+
#include <torch/types.h>
|
| 9 |
+
|
| 10 |
+
namespace torch {
|
| 11 |
+
namespace nn {
|
| 12 |
+
|
| 13 |
+
/// Applies fold over a 3-D input.
|
| 14 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Fold to learn about
|
| 15 |
+
/// the exact behavior of this module.
|
| 16 |
+
///
|
| 17 |
+
/// See the documentation for `torch::nn::FoldOptions` class to learn what
|
| 18 |
+
/// constructor arguments are supported for this module.
|
| 19 |
+
///
|
| 20 |
+
/// Example:
|
| 21 |
+
/// ```
|
| 22 |
+
/// Fold model(FoldOptions({8, 8}, {3, 3}).dilation(2).padding({2,
|
| 23 |
+
/// 1}).stride(2));
|
| 24 |
+
/// ```
|
| 25 |
+
class TORCH_API FoldImpl : public torch::nn::Cloneable<FoldImpl> {
|
| 26 |
+
public:
|
| 27 |
+
FoldImpl(ExpandingArray<2> output_size, ExpandingArray<2> kernel_size)
|
| 28 |
+
: FoldImpl(FoldOptions(output_size, kernel_size)) {}
|
| 29 |
+
explicit FoldImpl(const FoldOptions& options_);
|
| 30 |
+
|
| 31 |
+
void reset() override;
|
| 32 |
+
|
| 33 |
+
/// Pretty prints the `Fold` module into the given `stream`.
|
| 34 |
+
void pretty_print(std::ostream& stream) const override;
|
| 35 |
+
|
| 36 |
+
Tensor forward(const Tensor& input);
|
| 37 |
+
|
| 38 |
+
/// The options with which this `Module` was constructed.
|
| 39 |
+
FoldOptions options;
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
/// A `ModuleHolder` subclass for `FoldImpl`.
|
| 43 |
+
/// See the documentation for `FoldImpl` class to learn what methods it
|
| 44 |
+
/// provides, and examples of how to use `Fold` with `torch::nn::FoldOptions`.
|
| 45 |
+
/// See the documentation for `ModuleHolder` to learn about PyTorch's
|
| 46 |
+
/// module storage semantics.
|
| 47 |
+
TORCH_MODULE(Fold);
|
| 48 |
+
|
| 49 |
+
// ============================================================================
|
| 50 |
+
|
| 51 |
+
/// Applies unfold over a 4-D input.
|
| 52 |
+
/// See https://pytorch.org/docs/master/nn.html#torch.nn.Unfold to learn about
|
| 53 |
+
/// the exact behavior of this module.
|
| 54 |
+
///
|
| 55 |
+
/// See the documentation for `torch::nn::UnfoldOptions` class to learn what
|
| 56 |
+
/// constructor arguments are supported for this module.
|
| 57 |
+
///
|
| 58 |
+
/// Example:
|
| 59 |
+
/// ```
|
| 60 |
+
/// Unfold model(UnfoldOptions({2, 4}).dilation(2).padding({2, 1}).stride(2));
|
| 61 |
+
/// ```
|
| 62 |
+
class TORCH_API UnfoldImpl : public Cloneable<UnfoldImpl> {
|
| 63 |
+
public:
|
| 64 |
+
UnfoldImpl(ExpandingArray<2> kernel_size)
|
| 65 |
+
: UnfoldImpl(UnfoldOptions(kernel_size)) {}
|
| 66 |
+
explicit UnfoldImpl(const UnfoldOptions& options_);
|
| 67 |
+
|
| 68 |
+
void reset() override;
|
| 69 |
+
|
| 70 |
+
/// Pretty prints the `Unfold` module into the given `stream`.
|
| 71 |
+
void pretty_print(std::ostream& stream) const override;
|
| 72 |
+
|
| 73 |
+
Tensor forward(const Tensor& input);
|
| 74 |
+
|
| 75 |
+
/// The options with which this `Module` was constructed.
|
| 76 |
+
UnfoldOptions options;
|
| 77 |
+
};
|
| 78 |
+
|
| 79 |
+
/// A `ModuleHolder` subclass for `UnfoldImpl`.
|
| 80 |
+
/// See the documentation for `UnfoldImpl` class to learn what methods it
|
| 81 |
+
/// provides, and examples of how to use `Unfold` with
|
| 82 |
+
/// `torch::nn::UnfoldOptions`. See the documentation for `ModuleHolder` to
|
| 83 |
+
/// learn about PyTorch's module storage semantics.
|
| 84 |
+
TORCH_MODULE(Unfold);
|
| 85 |
+
|
| 86 |
+
} // namespace nn
|
| 87 |
+
} // namespace torch
|