<commit_before>//
//  TFWeightData.swift
//  Bender
//
//  Created by <NAME> on 5/19/17.
//
//

import Foundation

/// Internal struct used to pass weights and bias data around
struct TFWeightData {

    let weights: Data?
    let bias: Data?
    let weightShape: Tensorflow_TensorShapeProto
    let useBias: Bool

    static func getWeightData(node: TFNode) -> TFWeightData? {
        let varInputs = node.incomingNodes().cleanMap { ($0 as? TFNode) }.filter { $0.nodeDef.isTFVariableOrConstOp }
        var weightsVar: TFNode
        var biasVar: TFNode?

        if varInputs.count == 1 {
            weightsVar = varInputs[0]
        } else if varInputs.count == 2 {
            if varInputs[0].nodeDef.shape?.isBias == true {
                biasVar = varInputs[0]
                weightsVar = varInputs[1]
            } else if varInputs[1].nodeDef.shape?.isBias == true {
                biasVar = varInputs[1]
                weightsVar = varInputs[0]
            } else {
                fatalError("Conv2D(Transpose) must have 1 or 2 Variable input and one of them must be a bias")
            }
        } else {
            fatalError("Conv2D(Transpose) must have 1 or 2 Variable input")
        }

        guard let shape = weightsVar.nodeDef.shape else {
            fatalError("Conv2D(Transpose) has no shape information")
        }

        var weights: Data?
        var bias: Data?

        if weightsVar.nodeDef.isTFConstOp {
            weights = weightsVar.nodeDef.valueData()
        } else if let weightsNode = weightsVar.incomingNodes().first as? TFNode, weightsNode.nodeDef.isTFConstOp {
            weights = weightsNode.nodeDef.valueData()
        }

        if biasVar?.nodeDef.isTFConstOp == true {
            bias = biasVar?.nodeDef.valueData()
        } else if let biasNode = biasVar?.incomingNodes().first as? TFNode {
            bias = biasNode.nodeDef.valueData()
        }

        return TFWeightData(weights: weights, bias: bias, weightShape: shape, useBias: biasVar != nil)
    }

}<commit_msg>Be more flexible with weight inputs (#111)<commit_after>//
//  TFWeightData.swift
//  Bender
//
//  Created by <NAME> on 5/19/17.
//
//

import Foundation

/// Internal struct used to pass weights and bias data around
struct TFWeightData {

    let weights: Data?
    let bias: Data?
    let weightShape: Tensorflow_TensorShapeProto
    let useBias: Bool

    static func getWeightData(node: TFNode) -> TFWeightData? {
        let varInputs = node.incomingNodes().cleanMap { ($0 as? TFNode) }.filter { $0.nodeDef.isTFVariableOrConstOp }
        var weightsVar: TFNode
        var biasVar: TFNode?

        if varInputs.count == 1 {
            weightsVar = varInputs[0]
        } else if varInputs.count == 2 {
            if varInputs[0].nodeDef.shape?.isBias == true {
                biasVar = varInputs[0]
                weightsVar = varInputs[1]
            } else if varInputs[1].nodeDef.shape?.isBias == true {
                biasVar = varInputs[1]
                weightsVar = varInputs[0]
            } else {
                if varInputs[0].nodeDef.shape?.dim.count == 4 && varInputs[1].nodeDef.shape!.dim.isEmpty {
                    weightsVar = varInputs[0]
                } else if varInputs[1].nodeDef.shape?.dim.count == 4 && varInputs[0].nodeDef.shape!.dim.isEmpty {
                    weightsVar = varInputs[1]
                } else {
                    fatalError("Conv2D(Transpose) must have 1 or 2 Variable input and one of them must be a bias")
                }
            }
        } else {
            fatalError("Conv2D(Transpose) must have 1 or 2 Variable input")
        }

        guard let shape = weightsVar.nodeDef.shape else {
            fatalError("Conv2D(Transpose) has no shape information")
        }

        var weights: Data?
        var bias: Data?

        if weightsVar.nodeDef.isTFConstOp {
            weights = weightsVar.nodeDef.valueData()
        } else if let weightsNode = weightsVar.incomingNodes().first as? TFNode, weightsNode.nodeDef.isTFConstOp {
            weights = weightsNode.nodeDef.valueData()
        }

        if biasVar?.nodeDef.isTFConstOp == true {
            bias = biasVar?.nodeDef.valueData()
        } else if let biasNode = biasVar?.incomingNodes().first as? TFNode {
            bias = biasNode.nodeDef.valueData()
        }

        return TFWeightData(weights: weights, bias: bias, weightShape: shape, useBias: biasVar != nil)
    }

}
{'fraction_non_alphanumeric': 0.10168350168350168, 'fraction_numerical': 0.012794612794612794, 'mean_word_length': 2.0128465179175117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '666319', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1284, 'n_words': 334}