-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample method to data_pipeline (#20)
- Loading branch information
1 parent
5b47c8d
commit af213ff
Showing
9 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "fairseq2n/data/sample_data_source.h" | ||
|
||
#include <ATen/CPUGeneratorImpl.h> | ||
#include <ATen/Context.h> | ||
#include <ATen/Functions.h> | ||
|
||
#include <stdexcept> | ||
|
||
#include "fairseq2n/utils/tensor.h" | ||
|
||
namespace fairseq2n::detail { | ||
|
||
sample_data_source::sample_data_source(std::vector<data_pipeline> &&pipelines, std::vector<float32> &&weights) | ||
: pipelines_(std::move(pipelines)) | ||
{ | ||
weights_ = make_tensor_from_vector(weights, { static_cast<std::int64_t>(pipelines_.size()) }); | ||
generator_ = at::globalContext().defaultGenerator(c10::DeviceType::CPU); | ||
} | ||
|
||
std::optional<data> | ||
sample_data_source::next() | ||
{ | ||
if (eod_) | ||
return std::nullopt; | ||
|
||
std::optional<data> output = pipelines_[next_index()].next(); | ||
if (!output) | ||
eod_ = true; | ||
|
||
return output; | ||
} | ||
|
||
void | ||
sample_data_source::reset() | ||
{ | ||
eod_ = false; | ||
for (data_pipeline &p : pipelines_) | ||
p.reset(); | ||
} | ||
|
||
void | ||
sample_data_source::record_position(tape &t) const | ||
{ | ||
t.record(eod_); | ||
for (const data_pipeline &p : pipelines_) | ||
p.record_position(t); | ||
} | ||
|
||
void | ||
sample_data_source::reload_position(tape &t) | ||
{ | ||
eod_ = t.read<bool>(); | ||
for (data_pipeline &p : pipelines_) | ||
p.reload_position(t); | ||
} | ||
|
||
std::size_t | ||
sample_data_source::next_index() | ||
{ | ||
auto result = at::multinomial(weights_, 1, false, generator_) | ||
.item<std::int64_t>(); | ||
|
||
return static_cast<std::size_t>(result); | ||
} | ||
|
||
} // namespace fairseq2::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include <ATen/Generator.h> | ||
#include <ATen/Tensor.h> | ||
|
||
#include "fairseq2n/data/data_pipeline.h" | ||
#include "fairseq2n/data/data_source.h" | ||
|
||
namespace fairseq2n::detail { | ||
|
||
/// @brief sample from a list of datasources | ||
class sample_data_source final : public data_source { | ||
public: | ||
explicit | ||
sample_data_source(std::vector<data_pipeline> &&pipelines, std::vector<float32> &&weights); | ||
|
||
std::optional<data> | ||
next() override; | ||
|
||
void | ||
reset() override; | ||
|
||
void | ||
record_position(tape &t) const override; | ||
|
||
void | ||
reload_position(tape &t) override; | ||
|
||
private: | ||
std::size_t | ||
next_index(); | ||
|
||
private: | ||
std::vector<data_pipeline> pipelines_; | ||
bool eod_ = false; | ||
|
||
at::Generator generator_; | ||
at::Tensor weights_; | ||
}; | ||
|
||
} // namespace fairseq2::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include <ATen/Tensor.h> | ||
|
||
namespace fairseq2n::detail { | ||
|
||
template <typename T> | ||
inline at::Tensor | ||
make_tensor_from_vector( | ||
const std::vector<T> &src, | ||
const std::initializer_list<std::int64_t> &shape) noexcept | ||
{ | ||
auto storage = std::make_shared<std::vector<T>>(src); | ||
|
||
return at::from_blob( | ||
storage->data(), | ||
c10::ArrayRef<std::int64_t>(shape), | ||
[storage](void*) mutable { storage.reset(); } | ||
); | ||
} | ||
|
||
} // namespace fairseq2::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.