<commit_before>#include "xchainer/numerical_gradient.h"

#include <algorithm>
#include <functional>
#include <vector>

#ifdef XCHAINER_ENABLE_CUDA
#include <cuda_runtime.h>
#endif  // XCHAINER_ENABLE_CUDA

#include "xchainer/array.h"
#include "xchainer/array_repr.h"
#include "xchainer/error.h"
#include "xchainer/indexable_array.h"
#include "xchainer/indexer.h"
#ifdef XCHAINER_ENABLE_CUDA
#include "xchainer/cuda/cuda_runtime.h"
#endif  // XCHAINER_ENABLE_CUDA
#include "xchainer/device.h"

namespace xchainer {
namespace numerical_gradient_internal {

// TODO(niboshi): These temporary implementation for primitive operations depend on that the data in arrays can be accessed directly
// (e.g. with unified memory). In order for numerical gradient calculation to work corretly on general devices, They should be replaced with
// full-featured operations.

Array& Subtract(const Array& lhs, const Array& rhs, Array& out) {
    lhs.device().Synchronize();
    rhs.device().Synchronize();
    out.device().Synchronize();

    VisitDtype(lhs.dtype(), [&](auto pt) {
        using T = typename decltype(pt)::type;
        IndexableArray<const T> lhs_iarray{lhs};
        IndexableArray<const T> rhs_iarray{rhs};
        IndexableArray<T> out_iarray{out};
        Indexer indexer{out.shape()};

        for (int64_t i = 0; i < indexer.total_size(); ++i) {
            indexer.Set(i);
            out_iarray[indexer] = lhs_iarray[indexer] - rhs_iarray[indexer];
        }
    });
    return out;
}

Array& Divide(const Array& lhs, const Array& rhs, Array& out) {
    lhs.device().Synchronize();
    rhs.device().Synchronize();
    out.device().Synchronize();

    VisitDtype(lhs.dtype(), [&](auto pt) {
        using T = typename decltype(pt)::type;
        IndexableArray<const T> lhs_iarray{lhs};
        IndexableArray<const T> rhs_iarray{rhs};
        IndexableArray<T> out_iarray{out};
        Indexer indexer{out.shape()};

        for (int64_t i = 0; i < indexer.total_size(); ++i) {<commit_msg>Fix rebase error<commit_after>#include "xchainer/numerical_gradient.h"

#include <algorithm>
#include <functional>
#include <vector>

#ifdef XCHAINER_ENABLE_CUDA
#include <cuda_runtime.h>
#endif  // XCHAINER_ENABLE_CUDA

#include "xchainer/array.h"
#include "xchainer/array_repr.h"
#include "xchainer/error.h"
#include "xchainer/indexable_array.h"
#include "xchainer/indexer.h"
#ifdef XCHAINER_ENABLE_CUDA
#include "xchainer/cuda/cuda_runtime.h"
#endif  // XCHAINER_ENABLE_CUDA
#include "xchainer/device.h"

namespace xchainer {
namespace numerical_gradient_internal {

// TODO(niboshi): These temporary implementation for primitive operations depend on that the data in arrays can be accessed directly
// (e.g. with unified memory). In order for numerical gradient calculation to work corretly on general devices, They should be replaced with
// full-featured operations.

Array& Divide(const Array& lhs, const Array& rhs, Array& out) {
    lhs.device().Synchronize();
    rhs.device().Synchronize();
    out.device().Synchronize();

    VisitDtype(lhs.dtype(), [&](auto pt) {
        using T = typename decltype(pt)::type;
        IndexableArray<const T> lhs_iarray{lhs};
        IndexableArray<const T> rhs_iarray{rhs};
        IndexableArray<T> out_iarray{out};
        Indexer indexer{out.shape()};

        for (int64_t i = 0; i < indexer.total_size(); ++i) {
{'fraction_non_alphanumeric': 0.12696996729110913, 'fraction_numerical': 0.0026761819803746653, 'mean_word_length': 4.128048780487805, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6622055', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1145, 'n_words': 270}