Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback functions for input data. #5630

Closed
wants to merge 8 commits into from

Conversation

trivialfis
Copy link
Member

@trivialfis trivialfis commented May 5, 2020

@RAMitchell @hcho3 The first prototype.

My script:

import xgboost
import cupy
import json

# First prototype
class IterForCallbackTest(xgboost.core.DataIter):
    def __init__(self):
        self.rows = 100
        self.cols = 64
        rng = cupy.random.RandomState(1994)
        self._data = [rng.randn(self.rows, self.cols)] * 4
        self._weights = [rng.randn(self.rows)] * 4
        self._labels = [rng.randn(self.rows)] * 4

        self.it = 0
        super().__init__()

    def data(self):
        return self._data[self.it]

    def labels(self):
        return self._labels[self.it]

    def reset(self, this):
        self.it = 0

    def next(self, this, set_data, set_info):
        '''`this` is not used in Python.  ctypes can automatically remove
        remove `self` when converting a member function to c function
        pointer.

        '''
        if self.it == len(self._data):
            return 0
        interface = self.data().__cuda_array_interface__
        interface_str = bytes(json.dumps(interface, indent=2), 'utf-8')
        set_data(self.cxx_handle, interface_str)

        interface = self.labels().__cuda_array_interface__
        self.set_info_interface('label', interface, set_info)
        self.it += 1
        return 1

# Second prototype
class IterForDMatixArgTest(xgboost.core.DataIter):
    def __init__(self):
        self.rows = 100
        self.cols = 64
        rng = cupy.random.RandomState(1994)
        self._data = [rng.randn(self.rows, self.cols)] * 4
        self._weights = [rng.randn(self.rows)] * 4
        self._labels = [rng.randn(self.rows)] * 4

        self.it = 0
        super().__init__()

    def data(self):
        return self._data[self.it]

    def labels(self):
        return self._labels[self.it]

    def reset(self, this):
        self.it = 0

    def next(self, this, handle):
        if self.it == len(self._data):
            return 0
        m = xgboost.DMatrix(None)
        m.handle = handle
        m.set_interface_info('label', self.labels())
        m.set_data_from_interface(self.data())
        m.handle = None
        self.it += 1
        return 1


def test_iter():
    it = IterForDMatixArgTest()
    m = xgboost.DMatrix(it)
    print(m)


def test_normal():
    import numpy as np
    rng = np.random.RandomState(1994)
    X = rng.randn(10, 10)
    m = xgboost.DMatrix(X)
    print(m)


if __name__ == '__main__':
    # test_normal()
    test_iter()

@hcho3
Copy link
Collaborator

hcho3 commented May 9, 2020

@trivialfis Why are you closing this? Did you choose #5629?

@trivialfis
Copy link
Member Author

@hcho3 Yes. I think #5629 is much better than this one.

@trivialfis trivialfis deleted the dask-inplace-dmatrix branch September 18, 2020 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants