|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.sysds.runtime.instructions.cp; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | + |
| 24 | +import org.apache.sysds.common.Builtins; |
| 25 | +import org.apache.sysds.common.Types.ValueType; |
| 26 | +import org.apache.sysds.runtime.DMLRuntimeException; |
| 27 | +import org.apache.sysds.runtime.controlprogram.context.ExecutionContext; |
| 28 | +import org.apache.sysds.runtime.frame.data.FrameBlock; |
| 29 | +import org.apache.sysds.runtime.frame.data.columns.ColumnMetadata; |
| 30 | +import org.apache.sysds.runtime.matrix.data.MatrixBlock; |
| 31 | +import org.apache.sysds.runtime.matrix.operators.MultiThreadedOperator; |
| 32 | +import org.apache.sysds.runtime.transform.TfUtils.TfMethod; |
| 33 | +import org.apache.sysds.runtime.util.UtilFunctions; |
| 34 | +import org.apache.wink.json4j.JSONException; |
| 35 | +import org.apache.wink.json4j.JSONObject; |
| 36 | + |
| 37 | +public class BinaryFrameScalarCPInstruction extends BinaryCPInstruction { |
| 38 | + // private static final Log LOG = LogFactory.getLog(BinaryFrameFrameCPInstruction.class.getName()); |
| 39 | + |
| 40 | + private static final TfMethod[] UNSUPPORTED_MASK_METHODS = new TfMethod[] {TfMethod.BIN, |
| 41 | + TfMethod.WORD_EMBEDDING, TfMethod.BAG_OF_WORDS, TfMethod.UDF}; |
| 42 | + |
| 43 | + protected BinaryFrameScalarCPInstruction(MultiThreadedOperator op, CPOperand in1, CPOperand in2, CPOperand out, |
| 44 | + String opcode, String istr) { |
| 45 | + super(CPType.Binary, op, in1, in2, out, opcode, istr); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void processInstruction(ExecutionContext ec) { |
| 50 | + // get input frames |
| 51 | + FrameBlock inBlock1 = ec.getFrameInput(input1.getName()); |
| 52 | + ScalarObject spec = ec.getScalarInput(input2.getName(), ValueType.STRING, true); |
| 53 | + if(getOpcode().equals(Builtins.GET_CATEGORICAL_MASK.toString().toLowerCase())) { |
| 54 | + processGetCategorical(ec, inBlock1, spec); |
| 55 | + } |
| 56 | + else { |
| 57 | + throw new DMLRuntimeException("Unsupported operation"); |
| 58 | + } |
| 59 | + |
| 60 | + // Release the memory occupied by input frames |
| 61 | + ec.releaseFrameInput(input1.getName()); |
| 62 | + } |
| 63 | + |
| 64 | + private static void validate(JSONObject jSpec) { |
| 65 | + try { |
| 66 | + if(!jSpec.containsKey("ids") || !jSpec.getBoolean("ids")) |
| 67 | + throw new DMLRuntimeException("not supported non ID based spec for get_categorical_mask"); |
| 68 | + |
| 69 | + for(TfMethod m : UNSUPPORTED_MASK_METHODS) |
| 70 | + if(jSpec.containsKey(m.toString())) |
| 71 | + throw new DMLRuntimeException("unsupported transform method '" + m + "' for get_categorical_mask"); |
| 72 | + } |
| 73 | + catch(JSONException e) { |
| 74 | + throw new DMLRuntimeException(e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public void processGetCategorical(ExecutionContext ec, FrameBlock f, ScalarObject spec) { |
| 79 | + try { |
| 80 | + // 1. extract the spec, 2. validate it |
| 81 | + JSONObject jSpec = new JSONObject(spec.getStringValue()); |
| 82 | + validate(jSpec); |
| 83 | + |
| 84 | + // 3.-5. fold each supported transform method into the per-column mask state |
| 85 | + CategoricalMask mask = new CategoricalMask(f, jSpec); |
| 86 | + mask.hash(); |
| 87 | + mask.recode(); |
| 88 | + mask.dummycode(); |
| 89 | + |
| 90 | + // 6.-7. size and materialize the output mask |
| 91 | + ec.setMatrixOutput(output.getName(), mask.toMatrixBlock()); |
| 92 | + } |
| 93 | + catch(Exception e) { |
| 94 | + throw new DMLRuntimeException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Accumulates, per input column, how many output columns it expands to (lengths) and whether those |
| 100 | + * output columns are categorical (categorical). The arrays are allocated lazily: a column that no |
| 101 | + * method touches keeps the implicit default of a single, non-categorical output column. |
| 102 | + */ |
| 103 | + private static final class CategoricalMask { |
| 104 | + private final FrameBlock f; |
| 105 | + private final JSONObject jSpec; |
| 106 | + private final int nCol; |
| 107 | + |
| 108 | + private int[] lengths = null; |
| 109 | + private boolean[] categorical = null; |
| 110 | + |
| 111 | + // feature-hashed columns map to K buckets; a plain hashed column produces a single |
| 112 | + // (categorical) bucket-id column, while a hashed column that is additionally dummycoded |
| 113 | + // expands to K columns. |
| 114 | + private boolean[] hashed = null; |
| 115 | + private int K = 0; |
| 116 | + |
| 117 | + private CategoricalMask(FrameBlock f, JSONObject jSpec) { |
| 118 | + this.f = f; |
| 119 | + this.jSpec = jSpec; |
| 120 | + this.nCol = f.getNumColumns(); |
| 121 | + } |
| 122 | + |
| 123 | + private void hash() throws JSONException { |
| 124 | + String hash = TfMethod.HASH.toString(); |
| 125 | + if(!jSpec.containsKey(hash)) |
| 126 | + return; |
| 127 | + K = jSpec.getInt("K"); |
| 128 | + hashed = new boolean[nCol]; |
| 129 | + ensureCategorical(); |
| 130 | + for(Object aa : jSpec.getJSONArray(hash)) { |
| 131 | + int av = (Integer) aa - 1; |
| 132 | + hashed[av] = true; |
| 133 | + categorical[av] = true; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void recode() throws JSONException { |
| 138 | + String recode = TfMethod.RECODE.toString(); |
| 139 | + if(!jSpec.containsKey(recode)) |
| 140 | + return; |
| 141 | + ensureCategorical(); |
| 142 | + for(Object aa : jSpec.getJSONArray(recode)) { |
| 143 | + int av = (Integer) aa - 1; |
| 144 | + categorical[av] = true; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private void dummycode() throws JSONException { |
| 149 | + String dummycode = TfMethod.DUMMYCODE.toString(); |
| 150 | + if(!jSpec.containsKey(dummycode)) |
| 151 | + return; |
| 152 | + ensureCategorical(); |
| 153 | + ensureLengths(); |
| 154 | + for(Object aa : jSpec.getJSONArray(dummycode)) { |
| 155 | + int av = (Integer) aa - 1; |
| 156 | + lengths[av] = distinctCount(av); |
| 157 | + categorical[av] = true; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private int distinctCount(int av) { |
| 162 | + if(hashed != null && hashed[av]) |
| 163 | + // feature hashing followed by dummycoding yields K columns |
| 164 | + return K; |
| 165 | + ColumnMetadata d = f.getColumnMetadata()[av]; |
| 166 | + String v = f.getString(0, av); |
| 167 | + if(v.length() > 1 && v.charAt(0) == '¿') |
| 168 | + return UtilFunctions.parseToInt(v.substring(1)); |
| 169 | + return d.isDefault() ? 0 : (int) d.getNumDistinct(); |
| 170 | + } |
| 171 | + |
| 172 | + private int sumLengths() { |
| 173 | + if(lengths == null) |
| 174 | + return nCol; |
| 175 | + int sum = 0; |
| 176 | + for(int i = 0; i < nCol; i++) |
| 177 | + sum += lengths[i]; |
| 178 | + return sum; |
| 179 | + } |
| 180 | + |
| 181 | + private MatrixBlock toMatrixBlock() { |
| 182 | + MatrixBlock ret = new MatrixBlock(1, sumLengths(), false); |
| 183 | + ret.allocateDenseBlock(); |
| 184 | + int off = 0; |
| 185 | + for(int i = 0; i < nCol; i++) { |
| 186 | + int len = (lengths == null) ? 1 : lengths[i]; |
| 187 | + double val = (categorical != null && categorical[i]) ? 1 : 0; |
| 188 | + for(int j = 0; j < len; j++) |
| 189 | + ret.set(0, off++, val); |
| 190 | + } |
| 191 | + return ret; |
| 192 | + } |
| 193 | + |
| 194 | + private void ensureCategorical() { |
| 195 | + if(categorical == null) |
| 196 | + categorical = new boolean[nCol]; |
| 197 | + } |
| 198 | + |
| 199 | + private void ensureLengths() { |
| 200 | + if(lengths == null) { |
| 201 | + lengths = new int[nCol]; |
| 202 | + Arrays.fill(lengths, 1); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments