<commit_before>#include "xchainer/native/native_device.h"

#include <cassert>
#include <cstdint>

#ifdef XCHAINER_ENABLE_BLAS
#include <cblas.h>
#endif  // XCHAINER_ENABLE_BLAS

#include "xchainer/array.h"
#include "xchainer/device.h"
#include "xchainer/dtype.h"
#include "xchainer/indexable_array.h"
#include "xchainer/native/elementwise.h"
#include "xchainer/routines/creation.h"
#include "xchainer/shape.h"

namespace xchainer {
namespace native {

#ifdef XCHAINER_ENABLE_BLAS
namespace {

// Dispatch gemm routines based on the element type T
template <typename T>
struct GemmImpl;

template <>
struct GemmImpl<float> {
    template <typename... Args>
    void operator()(Args&&... args) const {
        cblas_sgemm(std::forward<Args>(args)...);
    }
};

template <>
struct GemmImpl<double> {
    template <typename... Args>
    void operator()(Args&&... args) const {
        cblas_dgemm(std::forward<Args>(args)...);
    }
};

struct GemmInputLayout {
    int64_t ld = 0;
    CBLAS_TRANSPOSE trans = CblasNoTrans;

    // Configure leading dimension and transposition accordingly, and makes the array C contiguous if necessary
    Array Configure(const Array& a) {
        assert(a.ndim() == 2);
        // Row-major
        // Note that this condition is slightly relaxed than Array::IsContiguous() which requires
        // a.strides()[0] == a.item_size() * a.shape()[1], that is,
        // a.strides()[0] % a.item_size() == 0 && a.strides()[0] / a.item_size() = a.shape()[1]
        if (a.strides()[1] == a.item_size() && a.strides()[0] % a.item_size() == 0) {
            ld = a.strides()[0] / a.item_size();
            return a;
        }
        // Column-major
        if (a.strides()[0] == a.item_size() && a.strides()[1] % a.item_size() == 0) {
            ld = a.strides()[1] / a.item_size();
            trans = CblasTrans;
            return a;
        }
        // Force row-major contiguous
        ld = a.shape()[1];
        return AsContiguousArray(a);
    }
};

void Gemm(const Array& a, const Array& b, const Array& out) {
    assert(a.ndim() == 2);
    assert(b.ndim() == 2);
    assert(out.ndim() == 2);
    assert(out.dtype() == Dtype::kFloat32 || out.dtype() == Dtype::kFloat64);

    int64_t m = a.shape()[0];
    int64_t k = a.shape()[1];
    int64_t n = b.shape()[1];<commit_msg>Fix conditions to make C contiguous<commit_after>#include "xchainer/native/native_device.h"

#include <cassert>
#include <cstdint>

#ifdef XCHAINER_ENABLE_BLAS
#include <cblas.h>
#endif  // XCHAINER_ENABLE_BLAS

#include "xchainer/array.h"
#include "xchainer/device.h"
#include "xchainer/dtype.h"
#include "xchainer/indexable_array.h"
#include "xchainer/native/elementwise.h"
#include "xchainer/routines/creation.h"
#include "xchainer/shape.h"

namespace xchainer {
namespace native {

#ifdef XCHAINER_ENABLE_BLAS
namespace {

// Dispatch gemm routines based on the element type T
template <typename T>
struct GemmImpl;

template <>
struct GemmImpl<float> {
    template <typename... Args>
    void operator()(Args&&... args) const {
        cblas_sgemm(std::forward<Args>(args)...);
    }
};

template <>
struct GemmImpl<double> {
    template <typename... Args>
    void operator()(Args&&... args) const {
        cblas_dgemm(std::forward<Args>(args)...);
    }
};

struct GemmInputLayout {
    int64_t ld = 0;
    CBLAS_TRANSPOSE trans = CblasNoTrans;

    // Configure leading dimension and transposition accordingly, and makes the array C contiguous if necessary
    Array Configure(const Array& a) {
        assert(a.ndim() == 2);
        // Row-major
        // Note that this condition is slightly relaxed than Array::IsContiguous() which requires
        // a.strides()[0] == a.item_size() * a.shape()[1]
        if (a.strides()[1] == a.item_size() && a.strides()[0] / a.item_size() >= a.shape()[1] && a.strides()[0] % a.item_size() == 0) {
            ld = a.strides()[0] / a.item_size();
            return a;
        }
        // Column-major
        if (a.strides()[0] == a.item_size() && a.strides()[1] / a.item_size() >= a.shape()[0] && a.strides()[1] % a.item_size() == 0) {
            ld = a.strides()[1] / a.item_size();
            trans = CblasTrans;
            return a;
        }
        // Force row-major contiguous
        ld = a.shape()[1];
        return AsContiguousArray(a);
    }
};

void Gemm(const Array& a, const Array& b, const Array& out) {
    assert(a.ndim() == 2);
    assert(b.ndim() == 2);
    assert(out.ndim() == 2);
    assert(out.dtype() == Dtype::kFloat32 || out.dtype() == Dtype::kFloat64);

    int64_t m = a.shape()[0];
    int64_t k = a.shape()[1];
    int64_t n = b.shape()[1];
{'fraction_non_alphanumeric': 0.16846186988367085, 'fraction_numerical': 0.015079707022834985, 'mean_word_length': 2.875626043405676, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '4506906', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1645, 'n_words': 364}